Checker relationships
Concept and boundary
Assignability is a directional relation, distinct from identity and runtime value validity. The checker considers structural members and special cases such as private origins and any. The public isTypeAssignableTo probe can illustrate a selected relation, not prove a formal theorem about every TypeScript type.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: isTypeRelatedTo (line 68367). 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: is-type-assignable-to
Revisit reference fixture ts-api-is-type-assignable-to (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,variable}=inspect('declare const narrow: "x"; declare const broad: string;');const a=c.getTypeAtLocation(variable("narrow").name), b=c.getTypeAtLocation(variable("broad").name);assert.equal(c.isTypeAssignableTo(a,b),true); assert.equal(c.isTypeAssignableTo(b,a),false);console.log("literal -> string: true; reverse: false");Expected stdout (exit 0):
literal -> string: true; reverse: falseWorkshop 2: get-property-of-type
Revisit reference fixture ts-api-get-property-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,variable}=inspect("declare const record: { id: number; [key: string]: number };");const n=variable("record").name, t=c.getTypeAtLocation(n);const s=c.getPropertyOfType(t,"id"); assert(s);assert.equal(c.typeToString(c.getTypeOfSymbolAtLocation(s,n)),"number");assert.equal(c.getPropertyOfType(t,"arbitrary"),undefined);console.log("id: number; arbitrary: no named symbol");Expected stdout (exit 0):
id: number; arbitrary: no named symbolPractice and recap
Compare a literal type with its primitive in both directions and add an unrelated object shape.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Checker relationships — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Checker relationships?
What does workshop 1 (is-type-assignable-to) 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('declare const narrow: "x"; declare const broad: string;');const a=c.getTypeAtLocation(variable("narrow").name), b=c.getTypeAtLocation(variable("broad").name);assert.equal(c.isTypeAssignableTo(a,b),true); assert.equal(c.isTypeAssignableTo(b,a),false);console.log("literal -> string: true; reverse: false");What does workshop 2 (get-property-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,variable}=inspect("declare const record: { id: number; [key: string]: number };");const n=variable("record").name, t=c.getTypeAtLocation(n);const s=c.getPropertyOfType(t,"id"); assert(s);assert.equal(c.typeToString(c.getTypeOfSymbolAtLocation(s,n)),"number");assert.equal(c.getPropertyOfType(t,"arbitrary"),undefined);console.log("id: number; arbitrary: no named symbol");