fix(49223): checker.getTypeAtLocation for ExpressionWithTypeArguments returns an error any type (#49284)

* fix(49223): handle ExpressionWithTypeArguments nodes in isExpressionNode

* Update src/compiler/utilities.ts

* Just use `!isHeritageClause(node.parent)`.

Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
This commit is contained in:
Oleksandr T
2022-06-03 03:28:36 +03:00
committed by GitHub
parent 34fe835f33
commit 9c8e6b53b8
8 changed files with 80 additions and 0 deletions

View File

@@ -2016,6 +2016,8 @@ namespace ts {
case SyntaxKind.AwaitExpression:
case SyntaxKind.MetaProperty:
return true;
case SyntaxKind.ExpressionWithTypeArguments:
return !isHeritageClause(node.parent);
case SyntaxKind.QualifiedName:
while (node.parent.kind === SyntaxKind.QualifiedName) {
node = node.parent;

View File

@@ -131,6 +131,30 @@ describe("unittests:: Public APIs:: getTypeAtLocation", () => {
assert.equal(type.flags, ts.TypeFlags.Any);
});
it("works on ExpressionWithTypeArguments", () => {
const content = `
function fn<T>(value: T) {
return { value };
}
const foo = fn<string>;
`;
const host = new fakes.CompilerHost(vfs.createFromFileSystem(
Harness.IO,
/*ignoreCase*/ true,
{ documents: [new documents.TextDocument("/file.ts", content)], cwd: "/" }));
const program = ts.createProgram({
host,
rootNames: ["/file.ts"],
options: { noLib: true }
});
const checker = program.getTypeChecker();
const file = program.getSourceFile("/file.ts")!;
const [declaration] = (ts.findLast(file.statements, ts.isVariableStatement) as ts.VariableStatement).declarationList.declarations;
assert.equal(checker.getTypeAtLocation(declaration.initializer!).flags, ts.TypeFlags.Object);
});
it("returns an errorType for VariableDeclaration with BindingPattern name", () => {
const content = "const foo = [1];\n" + "const [a] = foo;";