TypeScript Compiler API
getBaseTypes()
Returns direct resolved base types of a class or interface instance type. It does not recursively flatten the inheritance graph or list implements clauses as base classes. Use a suitable InterfaceType, not an arbitrary primitive or union. Baseline: TypeScript 5.8.3 and Node.js. The example is a local tooling script in a disposable directory; it writes temporary project files, not a ByteMind CodeRunner exercise.
Formal Syntax & Structure
typescript syntaxtypescript
checker.getBaseTypes(type: ts.InterfaceType): ts.BaseType[]Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| type | ts.InterfaceType | Class or interface semantic instance type. |
Working 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 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("interface Root { id:number } interface Mid extends Root {} interface Leaf extends Mid {}");const d=find(n=>ts.isInterfaceDeclaration(n)&&n.name.text==="Leaf");const t=c.getDeclaredTypeOfSymbol(c.getSymbolAtLocation(d.name));const bases=c.getBaseTypes(t); assert.deepEqual(bases.map(x=>c.typeToString(x)),["Mid"]);assert(c.getPropertyOfType(t,"id")); console.log("direct base: Mid; inherited id exists");
// Run: node example.cjs// Expected stdout: direct base: Mid; inherited id exists