Skip to content

Commit a5dae37

Browse files
authored
Prevent looking up symbol for as const from triggering an error (microsoft#48464)
1 parent df7ed82 commit a5dae37

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/compiler/checker.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,11 @@ namespace ts {
17901790
}
17911791
}
17921792

1793+
function isConstAssertion(location: Node) {
1794+
return (isAssertionExpression(location) && isConstTypeReference(location.type))
1795+
|| (isJSDocTypeTag(location) && isConstTypeReference(location.typeExpression));
1796+
}
1797+
17931798
/**
17941799
* Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and
17951800
* the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with
@@ -1831,6 +1836,11 @@ namespace ts {
18311836
let isInExternalModule = false;
18321837

18331838
loop: while (location) {
1839+
if (name === "const" && isConstAssertion(location)) {
1840+
// `const` in an `as const` has no symbol, but issues no error because there is no *actual* lookup of the type
1841+
// (it refers to the constant type of the expression instead)
1842+
return undefined;
1843+
}
18341844
// Locals of a source file are not in scope (because they get merged into the global symbol table)
18351845
if (location.locals && !isGlobalSourceFile(location)) {
18361846
if (result = lookup(location.locals, name, meaning)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////class Tex {
4+
//// type = 'Text' as /**/const;
5+
////}
6+
7+
verify.goToDefinition("", []);
8+
verify.noErrors();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////class Tex {
4+
//// type = </**/const>'Text';
5+
////}
6+
7+
verify.goToDefinition("", []);
8+
verify.noErrors();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @checkJs: true
4+
// @Filename: file.js
5+
////class Tex {
6+
//// type = (/** @type {/**/const} */'Text');
7+
////}
8+
9+
verify.goToDefinition("", []);
10+
verify.noErrors();

0 commit comments

Comments
 (0)