Skip to content
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

Prevent looking up symbol for as const from triggering an error #48464

Merged
merged 1 commit into from
Mar 28, 2022
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
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,11 @@ namespace ts {
}
}

function isConstAssertion(location: Node) {
return (isAssertionExpression(location) && isConstTypeReference(location.type))
|| (isJSDocTypeTag(location) && isConstTypeReference(location.typeExpression));
}

/**
* Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and
* the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with
Expand Down Expand Up @@ -1831,6 +1836,11 @@ namespace ts {
let isInExternalModule = false;

loop: while (location) {
if (name === "const" && isConstAssertion(location)) {
// `const` in an `as const` has no symbol, but issues no error because there is no *actual* lookup of the type
// (it refers to the constant type of the expression instead)
return undefined;
}
// Locals of a source file are not in scope (because they get merged into the global symbol table)
if (location.locals && !isGlobalSourceFile(location)) {
if (result = lookup(location.locals, name, meaning)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/fourslash/asConstRefsNoErrors1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />

////class Tex {
//// type = 'Text' as /**/const;
////}

verify.goToDefinition("", []);
verify.noErrors();
8 changes: 8 additions & 0 deletions tests/cases/fourslash/asConstRefsNoErrors2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />

////class Tex {
//// type = </**/const>'Text';
////}

verify.goToDefinition("", []);
verify.noErrors();
10 changes: 10 additions & 0 deletions tests/cases/fourslash/asConstRefsNoErrors3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

// @checkJs: true
// @Filename: file.js
////class Tex {
//// type = (/** @type {/**/const} */'Text');
////}

verify.goToDefinition("", []);
verify.noErrors();