diff --git a/_packages/native-preview/test/async/api.test.ts b/_packages/native-preview/test/async/api.test.ts index 197f9b9811..3da16ff421 100644 --- a/_packages/native-preview/test/async/api.test.ts +++ b/_packages/native-preview/test/async/api.test.ts @@ -2332,6 +2332,25 @@ describe("Checker - getTypeArguments", () => { await api.close(); } }); + + test("does not panic for a non-reference type (issue #4338)", async () => { + const api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": `export const s: string = "";`, + }); + try { + const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" }); + const project = snapshot.getProject("/tsconfig.json")!; + const symbol = await project.checker.getSymbolAtPosition("/src/main.ts", `export const `.length); + assert.ok(symbol); + const type = await project.checker.getTypeOfSymbol(symbol); + assert.ok(type); + assert.deepEqual(await project.checker.getTypeArguments(type), []); + } + finally { + await api.close(); + } + }); }); describe("TypeParameter - isThisType", () => { diff --git a/_packages/native-preview/test/sync/api.test.ts b/_packages/native-preview/test/sync/api.test.ts index e6a12e2196..7b833e0775 100644 --- a/_packages/native-preview/test/sync/api.test.ts +++ b/_packages/native-preview/test/sync/api.test.ts @@ -2340,6 +2340,25 @@ describe("Checker - getTypeArguments", () => { api.close(); } }); + + test("does not panic for a non-reference type (issue #4338)", () => { + const api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": `export const s: string = "";`, + }); + try { + const snapshot = api.updateSnapshot({ openProject: "/tsconfig.json" }); + const project = snapshot.getProject("/tsconfig.json")!; + const symbol = project.checker.getSymbolAtPosition("/src/main.ts", `export const `.length); + assert.ok(symbol); + const type = project.checker.getTypeOfSymbol(symbol); + assert.ok(type); + assert.deepEqual(project.checker.getTypeArguments(type), []); + } + finally { + api.close(); + } + }); }); describe("TypeParameter - isThisType", () => { diff --git a/internal/checker/exports.go b/internal/checker/exports.go index 516f5f149b..ae48373b9b 100644 --- a/internal/checker/exports.go +++ b/internal/checker/exports.go @@ -275,6 +275,12 @@ func (c *Checker) GetRestTypeOfSignature(sig *Signature) *Type { } func (c *Checker) GetTypeArguments(t *Type) []*Type { + // getTypeArguments assumes a type reference; non-reference types (e.g. the + // intrinsic `string`) would nil-deref through the public API. Match tsc's + // behavior of having no type arguments for them. + if t.objectFlags&ObjectFlagsReference == 0 { + return nil + } return c.getTypeArguments(t) }