Skip to content

Commit f8a09be

Browse files
authored
fix(48878): return errorType on invalid nodes in getTypeAtLocation (microsoft#48967)
1 parent 8e433cd commit f8a09be

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42198,7 +42198,7 @@ namespace ts {
4219842198
if (isDeclaration(node)) {
4219942199
// In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration
4220042200
const symbol = getSymbolOfNode(node);
42201-
return getTypeOfSymbol(symbol);
42201+
return symbol ? getTypeOfSymbol(symbol) : errorType;
4220242202
}
4220342203

4220442204
if (isDeclarationNameOrImportPropertyName(node)) {

src/testRunner/unittests/publicApi.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ describe("unittests:: Public APIs:: getTypeAtLocation", () => {
130130
const type = checker.getTypeAtLocation(file);
131131
assert.equal(type.flags, ts.TypeFlags.Any);
132132
});
133+
134+
it("returns an errorType for VariableDeclaration with BindingPattern name", () => {
135+
const content = "const foo = [1];\n" + "const [a] = foo;";
136+
137+
const host = new fakes.CompilerHost(vfs.createFromFileSystem(
138+
Harness.IO,
139+
/*ignoreCase*/ true,
140+
{ documents: [new documents.TextDocument("/file.ts", content)], cwd: "/" }));
141+
142+
const program = ts.createProgram({
143+
host,
144+
rootNames: ["/file.ts"],
145+
options: { noLib: true }
146+
});
147+
148+
const checker = program.getTypeChecker();
149+
const file = program.getSourceFile("/file.ts")!;
150+
const [declaration] = (ts.findLast(file.statements, ts.isVariableStatement) as ts.VariableStatement).declarationList.declarations;
151+
assert.equal(checker.getTypeAtLocation(declaration).flags, ts.TypeFlags.Any);
152+
});
133153
});
134154

135155
describe("unittests:: Public APIs:: validateLocaleAndSetLanguage", () => {

0 commit comments

Comments
 (0)