Skip to content

Fix getTypeAtLocation for as const to not issue a diagnostic #36741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11337,6 +11337,11 @@ namespace ts {
function getTypeFromTypeReference(node: TypeReferenceType): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this shouldn't getTypeOfNode just check if it is const of as const expression should forward it to correct node instead?

Copy link
Member Author

@weswigham weswigham Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehhhhhh, it could, but I'd rather push the handling to here, to prevent us from ever attempting to resolve the const in as const as a real type reference, however we may get there in the future (plus, by doing it here the result is cached).

// handle LS queries on the `const` in `x as const` by resolving to the type of `x`
if (isConstTypeReference(node) && isAssertionExpression(node.parent)) {
links.resolvedSymbol = unknownSymbol;
return links.resolvedType = checkExpressionCached(node.parent.expression);
}
let symbol: Symbol | undefined;
let type: Type | undefined;
const meaning = SymbolFlags.Type;
Expand Down
11 changes: 11 additions & 0 deletions src/testRunner/unittests/programApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ namespace ts {
});

describe("unittests:: programApi:: Program.getDiagnosticsProducingTypeChecker / Program.getSemanticDiagnostics", () => {
it("does not produce errors on `as const` it would not normally produce on the command line", () => {
const main = new documents.TextDocument("/main.ts", "0 as const");

const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main], cwd: "/" });
const program = createProgram(["/main.ts"], {}, new fakes.CompilerHost(fs, { newLine: NewLineKind.LineFeed }));
const typeChecker = program.getDiagnosticsProducingTypeChecker();
const sourceFile = program.getSourceFile("main.ts")!;
typeChecker.getTypeAtLocation(((sourceFile.statements[0] as ExpressionStatement).expression as AsExpression).type);
const diag = program.getSemanticDiagnostics();
assert.isEmpty(diag);
});
it("getSymbolAtLocation does not cause additional error to be added on module resolution failure", () => {
const main = new documents.TextDocument("/main.ts", "import \"./module\";");
const mod = new documents.TextDocument("/module.d.ts", "declare const foo: any;");
Expand Down