Conditional and mapped instantiation
Concept and boundary
Conditional and mapped types are instantiated as needed by semantic queries. Distributivity depends on a naked type parameter in the checked position; mapped keys determine available properties. Inspect resulting members and flags through public queries rather than assuming a particular internal cache layout.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: getConditionalType (line 66268); getMappedType (line 66949). 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-properties-of-type
Revisit reference fixture ts-api-get-properties-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 value: { shared: number; a: string } | { shared: number; b: boolean };");const t=c.getTypeAtLocation(variable("value").name);const names=c.getPropertiesOfType(t).map(s=>s.getName()).sort();assert.deepEqual(names,["shared"]); console.log(names.join(","));Expected stdout (exit 0):
sharedWorkshop 2: type-flags
Revisit reference fixture ts-api-type-flags (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 value: "x" | 7;');const t=c.getTypeAtLocation(variable("value").name);assert(t.flags & ts.TypeFlags.Union); assert(t.isUnion());assert(t.types.some(x=>x.flags & ts.TypeFlags.StringLiteral));assert(t.types.some(x=>x.flags & ts.TypeFlags.NumberLiteral));console.log("union contains string and number literals");Expected stdout (exit 0):
union contains string and number literalsWorkshop 3: conditional-mapped-probe
const ts = require("typescript");const assert = require("node:assert/strict");const p = ts.createProgram(["input.ts"], {types:[],strict:true,noLib:true});const c = p.getTypeChecker();const f = p.getSourceFile("input.ts");const alias = f.statements.find(n => ts.isTypeAliasDeclaration(n) && n.name.text === "Result");const type = c.getTypeAtLocation(alias.name);const names = c.getPropertiesOfType(type).map(s => s.name).sort();assert.deepEqual(names,["keep"]);console.log(names.join(","));type OnlyNumber<T> = {[K in keyof T as T[K] extends number ? K : never]: T[K]};type Result = OnlyNumber<{keep:number; omit:string}>;Expected stdout (exit 0):
keepPractice and recap
Construct a mapped union of records and query its common properties; compare a single member.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Conditional and mapped instantiation — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Conditional and mapped instantiation?
What does workshop 1 (get-properties-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 value: { shared: number; a: string } | { shared: number; b: boolean };");const t=c.getTypeAtLocation(variable("value").name);const names=c.getPropertiesOfType(t).map(s=>s.getName()).sort();assert.deepEqual(names,["shared"]); console.log(names.join(","));What does workshop 2 (type-flags) 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 value: "x" | 7;');const t=c.getTypeAtLocation(variable("value").name);assert(t.flags & ts.TypeFlags.Union); assert(t.isUnion());assert(t.types.some(x=>x.flags & ts.TypeFlags.StringLiteral));assert(t.types.some(x=>x.flags & ts.TypeFlags.NumberLiteral));console.log("union contains string and number literals");What does workshop 3 (conditional-mapped-probe) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const p = ts.createProgram(["input.ts"], {types:[],strict:true,noLib:true});const c = p.getTypeChecker();const f = p.getSourceFile("input.ts");const alias = f.statements.find(n => ts.isTypeAliasDeclaration(n) && n.name.text === "Result");const type = c.getTypeAtLocation(alias.name);const names = c.getPropertiesOfType(type).map(s => s.name).sort();assert.deepEqual(names,["keep"]);console.log(names.join(","));