Generic instantiation and inference
Concept and boundary
Inference gathers candidates in context and can instantiate a signature for the selected call. Constraints restrict permissible arguments but are not the inferred argument itself. Compare the resolved return type with declared type parameters and avoid treating internal candidate ordering as an API promise.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: inferTypes (line 72201). These are navigation anchors for source reading, not public API contracts. The probes below observe public behavior; they do not prove every internal step.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: get-return-type-of-signature
Revisit reference fixture ts-api-get-return-type-of-signature (historical V27, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,find}=inspect("function wrap<T>(value:T): {value:T} { return {value}; } const result=wrap<number>(7);");const s=c.getResolvedSignature(find(ts.isCallExpression)); assert(s);const t=c.getReturnTypeOfSignature(s);const p=c.getPropertyOfType(t,"value"); assert(p);assert.equal(c.typeToString(c.getTypeOfSymbolAtLocation(p,find(ts.isCallExpression))),"number");console.log("instantiated return property: number");Expected stdout (exit 0):
instantiated return property: numberWorkshop 2: get-type-arguments
Revisit reference fixture ts-api-get-type-arguments (historical V28, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,variable}=inspect("interface Pair<A,B> { first:A; second:B } declare const pair: Pair<string,number>;");const t=c.getTypeAtLocation(variable("pair").name);assert(t.flags & ts.TypeFlags.Object); assert(t.objectFlags & ts.ObjectFlags.Reference);const args=c.getTypeArguments(t).map(x=>c.typeToString(x));assert.deepEqual(args,["string","number"]); console.log(args.join(", "));Expected stdout (exit 0):
string, numberPractice and recap
Change the actual argument from a number to a string and record the resolved return type.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Generic instantiation and inference — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Generic instantiation and inference?
What does workshop 1 (get-return-type-of-signature) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,find}=inspect("function wrap<T>(value:T): {value:T} { return {value}; } const result=wrap<number>(7);");const s=c.getResolvedSignature(find(ts.isCallExpression)); assert(s);const t=c.getReturnTypeOfSignature(s);const p=c.getPropertyOfType(t,"value"); assert(p);assert.equal(c.typeToString(c.getTypeOfSymbolAtLocation(p,find(ts.isCallExpression))),"number");console.log("instantiated return property: number");What does workshop 2 (get-type-arguments) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,variable}=inspect("interface Pair<A,B> { first:A; second:B } declare const pair: Pair<string,number>;");const t=c.getTypeAtLocation(variable("pair").name);assert(t.flags & ts.TypeFlags.Object); assert(t.objectFlags & ts.ObjectFlags.Reference);const args=c.getTypeArguments(t).map(x=>c.typeToString(x));assert.deepEqual(args,["string","number"]); console.log(args.join(", "));