TypeScript Compiler API
ts.Type
An object representing a semantic type in the current checker, not a runtime JavaScript value. It exposes type flags, property symbols and signatures. Union checks and specialized guards help interpret different type representations. Do not persist internal identities or infer runtime data validation from a static type. Baseline: the Node.js API of TypeScript 5.8.3. The examples are CommonJS tooling scripts, not browser or CodeRunner snippets. TypeScript must be installed locally.
Formal Syntax & Structure
typescript syntaxtypescript
interface ts.Type; type.getProperties(): ts.Symbol[]; type.isUnion(): this is ts.UnionTypeWorking Example
Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const checker = program.getTypeChecker();const source = program.getSourceFile("input.ts");const declaration = source.statements[0].declarationList.declarations[0];const type = checker.getTypeAtLocation(declaration.name);const properties = type.getProperties().map(s => s.getName()).sort();assert.deepEqual(properties, ["count", "label"]);assert.equal(type.isUnion(), false);console.log(properties.join(","));
// File: input.ts (omit this label)const item = { count: 7, label: "box" };
// Run: node example.cjs// Expected stdout: count,label