TypeScript Compiler API
getTypeArguments()
Returns the semantic type arguments of a type reference, including instantiated arguments inferred by the compiler. Use a verified object reference type, not an arbitrary Type or a syntax TypeReferenceNode. A type alias may have separate alias type arguments; this query is not a general syntax argument enumerator. 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.getTypeArguments(type: ts.TypeReference): readonly ts.Type[]Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| type | ts.TypeReference | Instantiated semantic type reference. |
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,variable}=inspect("interface Pair<A,B> { first:A; second:B } declare const pair: Pair<string,number>;");const t=c.getTypeAtLocation(variable("pair").name);assert(t.flags & ts.TypeFlags.Object); assert(t.objectFlags & ts.ObjectFlags.Reference);const args=c.getTypeArguments(t).map(x=>c.typeToString(x));assert.deepEqual(args,["string","number"]); console.log(args.join(", "));
// Run: node example.cjs// Expected stdout: string, number