Skip to content

Commit 2a7398b

Browse files
committed
Include primitives in type-as-value error message
Previously, you would get the generic message when writing incorrect code like `let y = number`. "Cannot find name 'number'". Now the message says "'number' is a type but is used a value here." Fixes #15565
1 parent 9bf5209 commit 2a7398b

25 files changed

+96
-92
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,10 @@ namespace ts {
12361236

12371237
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags): boolean {
12381238
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
1239+
if (name === "any" || name === "string" || name === "number" || name === "boolean" || name === "void" || name === "never") {
1240+
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name);
1241+
return true;
1242+
}
12391243
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
12401244
if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) {
12411245
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name);

tests/baselines/reference/autoLift2.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
tests/cases/compiler/autoLift2.ts(5,14): error TS2339: Property 'foo' does not exist on type 'A'.
22
tests/cases/compiler/autoLift2.ts(5,17): error TS1005: ';' expected.
3-
tests/cases/compiler/autoLift2.ts(5,19): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
3+
tests/cases/compiler/autoLift2.ts(5,19): error TS2693: 'any' only refers to a type, but is being used as a value here.
44
tests/cases/compiler/autoLift2.ts(6,14): error TS2551: Property 'bar' does not exist on type 'A'. Did you mean 'baz'?
55
tests/cases/compiler/autoLift2.ts(6,17): error TS1005: ';' expected.
6-
tests/cases/compiler/autoLift2.ts(6,19): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
6+
tests/cases/compiler/autoLift2.ts(6,19): error TS2693: 'any' only refers to a type, but is being used as a value here.
77
tests/cases/compiler/autoLift2.ts(12,11): error TS2339: Property 'foo' does not exist on type 'A'.
88
tests/cases/compiler/autoLift2.ts(14,11): error TS2551: Property 'bar' does not exist on type 'A'. Did you mean 'baz'?
99
tests/cases/compiler/autoLift2.ts(16,33): error TS2339: Property 'foo' does not exist on type 'A'.
@@ -21,14 +21,14 @@ tests/cases/compiler/autoLift2.ts(18,33): error TS2551: Property 'bar' does not
2121
~
2222
!!! error TS1005: ';' expected.
2323
~~~
24-
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
24+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
2525
this.bar: any;
2626
~~~
2727
!!! error TS2551: Property 'bar' does not exist on type 'A'. Did you mean 'baz'?
2828
~
2929
!!! error TS1005: ';' expected.
3030
~~~
31-
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
31+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
3232
}
3333

3434

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/badArrayIndex.ts(1,15): error TS2552: Cannot find name 'number'. Did you mean 'Number'?
1+
tests/cases/compiler/badArrayIndex.ts(1,15): error TS2693: 'number' only refers to a type, but is being used as a value here.
22
tests/cases/compiler/badArrayIndex.ts(1,22): error TS1109: Expression expected.
33

44

55
==== tests/cases/compiler/badArrayIndex.ts (2 errors) ====
66
var results = number[];
77
~~~~~~
8-
!!! error TS2552: Cannot find name 'number'. Did you mean 'Number'?
8+
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
99
~
1010
!!! error TS1109: Expression expected.

tests/baselines/reference/bases.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
tests/cases/compiler/bases.ts(7,14): error TS2339: Property 'y' does not exist on type 'B'.
22
tests/cases/compiler/bases.ts(7,15): error TS1005: ';' expected.
3-
tests/cases/compiler/bases.ts(7,17): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
3+
tests/cases/compiler/bases.ts(7,17): error TS2693: 'any' only refers to a type, but is being used as a value here.
44
tests/cases/compiler/bases.ts(11,7): error TS2420: Class 'C' incorrectly implements interface 'I'.
55
Property 'x' is missing in type 'C'.
66
tests/cases/compiler/bases.ts(12,5): error TS2377: Constructors for derived classes must contain a 'super' call.
77
tests/cases/compiler/bases.ts(13,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
88
tests/cases/compiler/bases.ts(13,14): error TS2339: Property 'x' does not exist on type 'C'.
99
tests/cases/compiler/bases.ts(13,15): error TS1005: ';' expected.
10-
tests/cases/compiler/bases.ts(13,17): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
10+
tests/cases/compiler/bases.ts(13,17): error TS2693: 'any' only refers to a type, but is being used as a value here.
1111
tests/cases/compiler/bases.ts(17,9): error TS2339: Property 'x' does not exist on type 'C'.
1212
tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.
1313

@@ -25,7 +25,7 @@ tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist o
2525
~
2626
!!! error TS1005: ';' expected.
2727
~~~
28-
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
28+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
2929
}
3030
}
3131

@@ -44,7 +44,7 @@ tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist o
4444
~
4545
!!! error TS1005: ';' expected.
4646
~~~
47-
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
47+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
4848
}
4949
~~~~~
5050
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
1+
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2693: 'any' only refers to a type, but is being used as a value here.
22

33

44
==== tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts (1 errors) ====
55
var test: any[] = new any[1];
66
~~~
7-
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
7+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.

tests/baselines/reference/classExtendingPrimitive.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(3,17): error TS2552: Cannot find name 'number'. Did you mean 'Number'?
2-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(4,18): error TS2552: Cannot find name 'string'. Did you mean 'String'?
3-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(5,18): error TS2552: Cannot find name 'boolean'. Did you mean 'Boolean'?
1+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(3,17): error TS2693: 'number' only refers to a type, but is being used as a value here.
2+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(4,18): error TS2693: 'string' only refers to a type, but is being used as a value here.
3+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(5,18): error TS2693: 'boolean' only refers to a type, but is being used as a value here.
44
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(6,18): error TS2304: Cannot find name 'Void'.
55
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(7,19): error TS1109: Expression expected.
66
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(8,18): error TS2304: Cannot find name 'Null'.
@@ -14,13 +14,13 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla
1414

1515
class C extends number { }
1616
~~~~~~
17-
!!! error TS2552: Cannot find name 'number'. Did you mean 'Number'?
17+
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
1818
class C2 extends string { }
1919
~~~~~~
20-
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
20+
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
2121
class C3 extends boolean { }
2222
~~~~~~~
23-
!!! error TS2552: Cannot find name 'boolean'. Did you mean 'Boolean'?
23+
!!! error TS2693: 'boolean' only refers to a type, but is being used as a value here.
2424
class C4 extends Void { }
2525
~~~~
2626
!!! error TS2304: Cannot find name 'Void'.

tests/baselines/reference/classExtendsEveryObjectType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(4,17): error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'?
22
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,18): error TS2507: Type '{ foo: any; }' is not a constructor function type.
3-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,25): error TS2552: Cannot find name 'string'. Did you mean 'String'?
3+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,25): error TS2693: 'string' only refers to a type, but is being used as a value here.
44
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,31): error TS1005: ',' expected.
55
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(8,18): error TS2507: Type '{ foo: string; }' is not a constructor function type.
66
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(11,18): error TS2507: Type 'typeof M' is not a constructor function type.
@@ -20,7 +20,7 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla
2020
~~~~~~~~~~~~~~~~
2121
!!! error TS2507: Type '{ foo: any; }' is not a constructor function type.
2222
~~~~~~
23-
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
23+
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
2424
~
2525
!!! error TS1005: ',' expected.
2626
var x: { foo: string; }

tests/baselines/reference/classExtendsEveryObjectType2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,18): error TS2507: Type '{ foo: any; }' is not a constructor function type.
2-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,25): error TS2552: Cannot find name 'string'. Did you mean 'String'?
2+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,25): error TS2693: 'string' only refers to a type, but is being used as a value here.
33
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,31): error TS1005: ',' expected.
44
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(3,18): error TS2507: Type 'undefined[]' is not a constructor function type.
55

@@ -9,7 +9,7 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla
99
~~~~~~~~~~~~~~~~
1010
!!! error TS2507: Type '{ foo: any; }' is not a constructor function type.
1111
~~~~~~
12-
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
12+
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
1313
~
1414
!!! error TS1005: ',' expected.
1515

tests/baselines/reference/classMemberWithMissingIdentifier2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,11): error TS1146: D
22
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,12): error TS1005: '=' expected.
33
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,14): error TS2304: Cannot find name 'name'.
44
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,18): error TS1005: ']' expected.
5-
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,19): error TS2552: Cannot find name 'string'. Did you mean 'String'?
5+
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,19): error TS2693: 'string' only refers to a type, but is being used as a value here.
66
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,25): error TS1005: ',' expected.
77
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,26): error TS1136: Property assignment expected.
88
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,27): error TS2304: Cannot find name 'VariableDeclaration'.
@@ -20,7 +20,7 @@ tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,27): error TS2304: C
2020
~
2121
!!! error TS1005: ']' expected.
2222
~~~~~~
23-
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
23+
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
2424
~
2525
!!! error TS1005: ',' expected.
2626
~

tests/baselines/reference/complicatedPrivacy.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/complicatedPrivacy.ts(11,24): error TS1054: A 'get' accessor cannot have parameters.
22
tests/cases/compiler/complicatedPrivacy.ts(35,5): error TS1170: A computed property name in a type literal must directly refer to a built-in symbol.
3-
tests/cases/compiler/complicatedPrivacy.ts(35,6): error TS2552: Cannot find name 'number'. Did you mean 'Number'?
3+
tests/cases/compiler/complicatedPrivacy.ts(35,6): error TS2693: 'number' only refers to a type, but is being used as a value here.
44
tests/cases/compiler/complicatedPrivacy.ts(73,55): error TS2694: Namespace 'mglo5' has no exported member 'i6'.
55

66

@@ -45,7 +45,7 @@ tests/cases/compiler/complicatedPrivacy.ts(73,55): error TS2694: Namespace 'mglo
4545
~~~~~~~~
4646
!!! error TS1170: A computed property name in a type literal must directly refer to a built-in symbol.
4747
~~~~~~
48-
!!! error TS2552: Cannot find name 'number'. Did you mean 'Number'?
48+
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
4949
}) {
5050
}
5151

0 commit comments

Comments
 (0)