Skip to content

typescript/async: getSourceFile() returns a NumberKeyword for a typeToTypeNode() result #65457

Open

Description

@mathcovax

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

  1. markstone111 commented on Nov 15, 2026

    @markstone111

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: APIRelates to the public API for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions