Repository navigation
Description
getSourceFile() returns the synthesized node itself on a NumberKeyword produced by checker.typeToTypeNode(). The method is declared to return a SourceFile, but the returned object has kind === SyntaxKind.NumberKeyword and text === undefined.
Reproduced with typescript@7.1.0-beta (3e9236361fa97a9e6c685f4833ac457ae2d8c50a) and Node.js v24.18.0 on macOS arm64.
Reproduction
Install typescript@7.1.0-beta, create the two files below in the same directory, and run node repro.mjs. No tsconfig or other dependencies are needed.
input.ts:
function add(one: number, two: number) { return one + two; }repro.mjs:
import { API } from 'typescript/async';
import { SyntaxKind } from 'typescript/ast';
const cwd = process.cwd();
const api = new API({ cwd });
try {
const entry = `${cwd}/input.ts`;
const program = await api.createProgram([entry], { strict: true });
const checker = program.getProject().checker;
const source = await program.getSourceFile(entry);
const declaration = source.statements[0];
const signature = await checker.getSignatureFromDeclaration(declaration);
const type = await checker.getReturnTypeOfSignature(signature);
const node = await checker.typeToTypeNode(type, declaration);
console.log('inferred type:', await checker.typeToString(type));
console.log('node:', SyntaxKind[node.kind], node.pos, node.end);
const file = node.getSourceFile();
console.log('getSourceFile kind:', SyntaxKind[file.kind]);
console.log('getSourceFile is node:', file === node);
console.log('getSourceFile text:', file.text);
console.log('printed:', await api.printer.printNode(node));
console.log(node.getText());
} finally {
await api.close();
}Actual behavior
inferred type: number
node: NumberKeyword -1 -1
getSourceFile kind: NumberKeyword
getSourceFile is node: true
getSourceFile text: undefined
printed: number
TypeError: Cannot read properties of undefined (reading 'substring')
at RemoteSourceFile.getText (.../typescript/dist/api/node/node.generated.js:294:32)
getText() throws when it tries to call substring() on the missing source text. api.printer.printNode(node) prints number as expected.
Expected behavior
If a synthesized node has no source file, the API should signal that absence rather than return the synthesized node as a SourceFile. #24273 covers the existing limitation of getText() on synthesized nodes; this report is about the value returned by getSourceFile().
This report was drafted with AI assistance. The reproduction was run locally.
Activity
markstone111 commentedon Nov 15, 2026
Hi mathcovax,
I'd like to work on this issue. I can investigate the synthesized node handling in getSourceFile() and work on a fix that correctly represents the absence of an associated SourceFile, along with appropriate regression tests.
Could a maintainer please confirm the proposed approach and assign this issue to me? I'm happy to take it up.