Skip to content

Commit 31374d2

Browse files
authored
Provide suggestions for common can-not-find-name errors (#27034)
1 parent 1c13792 commit 31374d2

File tree

48 files changed

+544
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+544
-133
lines changed

src/compiler/checker.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,10 @@ namespace ts {
16571657
}
16581658
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
16591659
if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) {
1660-
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, unescapeLeadingUnderscores(name));
1660+
const message = (name === "Promise" || name === "Symbol")
1661+
? Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later
1662+
: Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here;
1663+
error(errorLocation, message, unescapeLeadingUnderscores(name));
16611664
return true;
16621665
}
16631666
}
@@ -2081,7 +2084,7 @@ namespace ts {
20812084
const namespaceMeaning = SymbolFlags.Namespace | (isInJavaScriptFile(name) ? meaning & SymbolFlags.Value : 0);
20822085
let symbol: Symbol | undefined;
20832086
if (name.kind === SyntaxKind.Identifier) {
2084-
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : Diagnostics.Cannot_find_name_0;
2087+
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : getCannotFindNameDiagnosticForName(getFirstIdentifier(name).escapedText);
20852088
const symbolFromJSPrototype = isInJavaScriptFile(name) ? resolveEntityNameFromJSSpecialAssignment(name, meaning) : undefined;
20862089
symbol = resolveName(location || name, name.escapedText, meaning, ignoreErrors || symbolFromJSPrototype ? undefined : message, name, /*isUse*/ true);
20872090
if (!symbol) {
@@ -13842,6 +13845,36 @@ namespace ts {
1384213845

1384313846
// EXPRESSION TYPE CHECKING
1384413847

13848+
function getCannotFindNameDiagnosticForName(name: __String): DiagnosticMessage {
13849+
switch (name) {
13850+
case "document":
13851+
case "console":
13852+
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom;
13853+
case "$":
13854+
return Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_types_Slashjquery;
13855+
case "describe":
13856+
case "suite":
13857+
case "it":
13858+
case "test":
13859+
return Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_types_Slashjest_or_npm_i_types_Slashmocha;
13860+
case "process":
13861+
case "require":
13862+
case "Buffer":
13863+
case "module":
13864+
return Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_types_Slashnode;
13865+
case "Map":
13866+
case "Set":
13867+
case "Promise":
13868+
case "Symbol":
13869+
case "WeakMap":
13870+
case "WeakSet":
13871+
case "Iterator":
13872+
case "AsyncIterator":
13873+
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later;
13874+
default: return Diagnostics.Cannot_find_name_0;
13875+
}
13876+
}
13877+
1384513878
function getResolvedSymbol(node: Identifier): Symbol {
1384613879
const links = getNodeLinks(node);
1384713880
if (!links.resolvedSymbol) {
@@ -13850,7 +13883,7 @@ namespace ts {
1385013883
node,
1385113884
node.escapedText,
1385213885
SymbolFlags.Value | SymbolFlags.ExportValue,
13853-
Diagnostics.Cannot_find_name_0,
13886+
getCannotFindNameDiagnosticForName(node.escapedText),
1385413887
node,
1385513888
!isWriteOnlyAccess(node),
1385613889
/*excludeGlobals*/ false,

src/compiler/diagnosticMessages.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,30 @@
20882088
"category": "Error",
20892089
"code": 2577
20902090
},
2091+
"Cannot find name '{0}'. Do you need to install type definitions for node? Try `npm i @types/node`.": {
2092+
"category": "Error",
2093+
"code": 2580
2094+
},
2095+
"Cannot find name '{0}'. Do you need to install type definitions for jQuery? Try `npm i @types/jquery`.": {
2096+
"category": "Error",
2097+
"code": 2581
2098+
},
2099+
"Cannot find name '{0}'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.": {
2100+
"category": "Error",
2101+
"code": 2582
2102+
},
2103+
"Cannot find name '{0}'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.": {
2104+
"category": "Error",
2105+
"code": 2583
2106+
},
2107+
"Cannot find name '{0}'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.": {
2108+
"category": "Error",
2109+
"code": 2584
2110+
},
2111+
"'{0}' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.": {
2112+
"category": "Error",
2113+
"code": 2585
2114+
},
20912115
"JSX element attributes type '{0}' may not be a union type.": {
20922116
"category": "Error",
20932117
"code": 2600

tests/baselines/reference/ES5For-ofTypeCheck10.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(9,6): error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
1+
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(9,6): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
22
tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(14,15): error TS2569: Type 'StringIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
33

44

@@ -13,7 +13,7 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck10.ts(14,1
1313
}
1414
[Symbol.iterator]() {
1515
~~~~~~
16-
!!! error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
16+
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
1717
return this;
1818
}
1919
}

tests/baselines/reference/ES5SymbolProperty2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(5,10): error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.
2-
tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(10,11): error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
2+
tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(10,11): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
33

44

55
==== tests/cases/conformance/Symbols/ES5SymbolProperty2.ts (2 errors) ====
@@ -16,4 +16,4 @@ tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(10,11): error TS2693: 'Sym
1616

1717
(new M.C)[Symbol.iterator];
1818
~~~~~~
19-
!!! error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
19+
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(2,6): error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
2-
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(5,9): error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
1+
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(2,6): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
2+
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(5,9): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
33

44

55
==== tests/cases/conformance/Symbols/ES5SymbolProperty6.ts (2 errors) ====
66
class C {
77
[Symbol.iterator]() { }
88
~~~~~~
9-
!!! error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
9+
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
1010
}
1111

1212
(new C)[Symbol.iterator]
1313
~~~~~~
14-
!!! error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
14+
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

tests/baselines/reference/anonymousModules.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
tests/cases/compiler/anonymousModules.ts(1,1): error TS2304: Cannot find name 'module'.
1+
tests/cases/compiler/anonymousModules.ts(1,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
22
tests/cases/compiler/anonymousModules.ts(1,8): error TS1005: ';' expected.
3-
tests/cases/compiler/anonymousModules.ts(4,2): error TS2304: Cannot find name 'module'.
3+
tests/cases/compiler/anonymousModules.ts(4,2): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
44
tests/cases/compiler/anonymousModules.ts(4,9): error TS1005: ';' expected.
5-
tests/cases/compiler/anonymousModules.ts(10,2): error TS2304: Cannot find name 'module'.
5+
tests/cases/compiler/anonymousModules.ts(10,2): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
66
tests/cases/compiler/anonymousModules.ts(10,9): error TS1005: ';' expected.
77

88

99
==== tests/cases/compiler/anonymousModules.ts (6 errors) ====
1010
module {
1111
~~~~~~
12-
!!! error TS2304: Cannot find name 'module'.
12+
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
1313
~
1414
!!! error TS1005: ';' expected.
1515
export var foo = 1;
1616

1717
module {
1818
~~~~~~
19-
!!! error TS2304: Cannot find name 'module'.
19+
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
2020
~
2121
!!! error TS1005: ';' expected.
2222
export var bar = 1;
@@ -26,7 +26,7 @@ tests/cases/compiler/anonymousModules.ts(10,9): error TS1005: ';' expected.
2626

2727
module {
2828
~~~~~~
29-
!!! error TS2304: Cannot find name 'module'.
29+
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
3030
~
3131
!!! error TS1005: ';' expected.
3232
var x = bar;

tests/baselines/reference/argumentsObjectIterator02_ES5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/compiler/argumentsObjectIterator02_ES5.ts(2,26): error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
1+
tests/cases/compiler/argumentsObjectIterator02_ES5.ts(2,26): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
22

33

44
==== tests/cases/compiler/argumentsObjectIterator02_ES5.ts (1 errors) ====
55
function doubleAndReturnAsArray(x: number, y: number, z: number): [number, number, number] {
66
let blah = arguments[Symbol.iterator];
77
~~~~~~
8-
!!! error TS2693: 'Symbol' only refers to a type, but is being used as a value here.
8+
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
99

1010
let result = [];
1111
for (let arg of blah()) {

tests/baselines/reference/conflictingCommonJSES2015Exports.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/conformance/salsa/bug24934.js(2,1): error TS2304: Cannot find name 'module'.
1+
tests/cases/conformance/salsa/bug24934.js(2,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
22

33

44
==== tests/cases/conformance/salsa/bug24934.js (1 errors) ====
55
export function abc(a, b, c) { return 5; }
66
module.exports = { abc };
77
~~~~~~
8-
!!! error TS2304: Cannot find name 'module'.
8+
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
99
==== tests/cases/conformance/salsa/use.js (0 errors) ====
1010
import { abc } from './bug24934';
1111
abc(1, 2, 3);

tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2304: Cannot find name 'module'.
21
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2503: Cannot find namespace 'module'.
2+
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
33
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(11,19): error TS1005: ';' expected.
44
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(22,35): error TS1005: ')' expected.
55
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(22,39): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
@@ -21,8 +21,8 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(40,41): error TS
2121
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(40,45): error TS1002: Unterminated string literal.
2222
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(41,21): error TS2304: Cannot find name 'retValue'.
2323
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(46,13): error TS1005: 'try' expected.
24-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(47,17): error TS2304: Cannot find name 'console'.
25-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(53,13): error TS2304: Cannot find name 'console'.
24+
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(47,17): error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.
25+
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(53,13): error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.
2626
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(58,5): error TS1128: Declaration or statement expected.
2727
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(69,13): error TS1109: Expression expected.
2828
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(72,37): error TS1127: Invalid character.
@@ -103,9 +103,9 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(261,1): error TS
103103

104104
import fs = module("fs");
105105
~~~~~~
106-
!!! error TS2304: Cannot find name 'module'.
107-
~~~~~~
108106
!!! error TS2503: Cannot find namespace 'module'.
107+
~~~~~~
108+
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
109109
~
110110
!!! error TS1005: ';' expected.
111111

@@ -188,15 +188,15 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(261,1): error TS
188188
!!! error TS1005: 'try' expected.
189189
console.log(e);
190190
~~~~~~~
191-
!!! error TS2304: Cannot find name 'console'.
191+
!!! error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.
192192
}
193193
finally {
194194

195195
}
196196

197197
console.log('Done');
198198
~~~~~~~
199-
!!! error TS2304: Cannot find name 'console'.
199+
!!! error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.
200200

201201
return 0;
202202

tests/baselines/reference/decoratorMetadataNoLibIsolatedModulesTypes.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ error TS2318: Cannot find global type 'Object'.
77
error TS2318: Cannot find global type 'RegExp'.
88
error TS2318: Cannot find global type 'String'.
99
tests/cases/compiler/decoratorMetadataNoLibIsolatedModulesTypes.ts(2,6): error TS2304: Cannot find name 'Decorate'.
10-
tests/cases/compiler/decoratorMetadataNoLibIsolatedModulesTypes.ts(3,13): error TS2304: Cannot find name 'Map'.
10+
tests/cases/compiler/decoratorMetadataNoLibIsolatedModulesTypes.ts(3,13): error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
1111

1212

1313
!!! error TS2318: Cannot find global type 'Array'.
@@ -25,6 +25,6 @@ tests/cases/compiler/decoratorMetadataNoLibIsolatedModulesTypes.ts(3,13): error
2525
!!! error TS2304: Cannot find name 'Decorate'.
2626
member: Map<string, number>;
2727
~~~
28-
!!! error TS2304: Cannot find name 'Map'.
28+
!!! error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
2929
}
3030

0 commit comments

Comments
 (0)