Types and signatures
Concept and boundary
Inspect call and construct signatures separately; overload declarations may provide multiple call signatures. getResolvedSignature describes the signature chosen for a call, and a generic call may instantiate its return type. Printed type strings are useful for humans, not stable identity keys.
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-signatures-of-type
Revisit reference fixture ts-api-get-signatures-of-type (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 convert(x: string): number;function convert(x: number): string;function convert(x: string | number): string | number { return typeof x === "string" ? x.length : String(x); }`);const t=c.getTypeAtLocation(find(ts.isFunctionDeclaration).name);assert.equal(c.getSignaturesOfType(t,ts.SignatureKind.Call).length,2);assert.equal(c.getSignaturesOfType(t,ts.SignatureKind.Construct).length,0);console.log("2 call signatures; 0 construct signatures");Expected stdout (exit 0):
2 call signatures; 0 construct signaturesWorkshop 2: 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: numberPractice and recap
Add a boolean overload and inspect the chosen signature at a call.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Types and signatures — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Types and signatures?
What does workshop 1 (get-signatures-of-type) 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 convert(x: string): number;function convert(x: number): string;function convert(x: string | number): string | number { return typeof x === "string" ? x.length : String(x); }`);const t=c.getTypeAtLocation(find(ts.isFunctionDeclaration).name);assert.equal(c.getSignaturesOfType(t,ts.SignatureKind.Call).length,2);assert.equal(c.getSignaturesOfType(t,ts.SignatureKind.Construct).length,0);console.log("2 call signatures; 0 construct signatures");What does workshop 2 (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");