Skip to content

getTypeAtLocation crashes on the ImportClause of a type-only import #64467

Description

@lsh4711

🔎 Search Terms

getTypeAtLocation ImportClause, type-only import crash, import type getTypeAtLocation nil pointer, tryGetDeclaredTypeOfSymbol, getDeclaredTypeOfSymbol panic

🕗 Version & Regression Information

  • This is the behavior in every version I tried: 7.1.0-dev.20260926.1 (panics in the Go checker) and 6.0.3 (throws TypeError: Cannot read properties of undefined (reading 'flags')). The unguarded branch looks like it dates back to Type-only imports and exports #35200, which added type-only ImportClause to isTypeDeclaration.

⏯ Playground Link

No response

💻 Code

// types.ts
export type U = number;
// main.ts
import type { U } from "./types";
export const u: U = 1;
const clause = program.getSourceFile("main.ts").statements[0].importClause;
checker.getTypeAtLocation(clause);

🙁 Actual behavior

With TypeScript 7, the API server panics:

panic: runtime error: invalid memory address or nil pointer dereference
checker.(*Checker).tryGetDeclaredTypeOfSymbol
checker.(*Checker).getDeclaredTypeOfSymbol
checker.(*Checker).getTypeOfNode
checker.(*Checker).GetTypeAtLocation

With TypeScript 6.0.3, getTypeAtLocation throws TypeError: Cannot read properties of undefined (reading 'flags').

The same happens for import type * as ns from "./types". A regular import { U } from "./types" clause does not crash.

🙂 Expected behavior

getTypeAtLocation returns a type (the error type, like it does for a regular import clause) instead of crashing.

Additional information about the issue

I'm building syscript, a compiler that compiles TypeScript syntax to C for systems programming. It walks every node of a program and requests its type through the TypeScript 7 API, and that walk crashed on the first file that used import type.

isTypeDeclaration returns true for a type-only ImportClause, but a clause only has a symbol when it has a default binding, so getTypeOfNode passes a nil symbol to getDeclaredTypeOfSymbol. I have a fix with a test ready and will open a PR referencing this issue.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions