Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions _packages/native-preview/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
19 changes: 19 additions & 0 deletions _packages/native-preview/test/sync/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
6 changes: 6 additions & 0 deletions internal/checker/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down