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

This commit is contained in:
Wesley Wigham 2022-03-28 12:53:36 -07:00 committed by GitHub
parent df7ed82dd5
commit a5dae37943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 0 deletions

View File

@ -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
@ -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)) {

View File

@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
////class Tex {
//// type = 'Text' as /**/const;
////}
verify.goToDefinition("", []);
verify.noErrors();

View File

@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
////class Tex {
//// type = </**/const>'Text';
////}
verify.goToDefinition("", []);
verify.noErrors();

View File

@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />
// @checkJs: true
// @Filename: file.js
////class Tex {
//// type = (/** @type {/**/const} */'Text');
////}
verify.goToDefinition("", []);
verify.noErrors();