Skip to content

Commit

Permalink
Include primitives in type-as-value error message
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sandersn committed May 3, 2017
1 parent 9bf5209 commit 2a7398b
Show file tree
Hide file tree
Showing 25 changed files with 96 additions and 92 deletions.
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,10 @@ namespace ts {

function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags): boolean {
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
if (name === "any" || name === "string" || name === "number" || name === "boolean" || name === "void" || name === "never") {
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name);
return true;
}
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) {
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name);
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/autoLift2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/compiler/autoLift2.ts(5,14): error TS2339: Property 'foo' does not exist on type 'A'.
tests/cases/compiler/autoLift2.ts(5,17): error TS1005: ';' expected.
tests/cases/compiler/autoLift2.ts(5,19): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
tests/cases/compiler/autoLift2.ts(5,19): error TS2693: 'any' only refers to a type, but is being used as a value here.
tests/cases/compiler/autoLift2.ts(6,14): error TS2551: Property 'bar' does not exist on type 'A'. Did you mean 'baz'?
tests/cases/compiler/autoLift2.ts(6,17): error TS1005: ';' expected.
tests/cases/compiler/autoLift2.ts(6,19): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
tests/cases/compiler/autoLift2.ts(6,19): error TS2693: 'any' only refers to a type, but is being used as a value here.
tests/cases/compiler/autoLift2.ts(12,11): error TS2339: Property 'foo' does not exist on type 'A'.
tests/cases/compiler/autoLift2.ts(14,11): error TS2551: Property 'bar' does not exist on type 'A'. Did you mean 'baz'?
tests/cases/compiler/autoLift2.ts(16,33): error TS2339: Property 'foo' does not exist on type 'A'.
Expand All @@ -21,14 +21,14 @@ tests/cases/compiler/autoLift2.ts(18,33): error TS2551: Property 'bar' does not
~
!!! error TS1005: ';' expected.
~~~
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
this.bar: any;
~~~
!!! error TS2551: Property 'bar' does not exist on type 'A'. Did you mean 'baz'?
~
!!! error TS1005: ';' expected.
~~~
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
}


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


==== tests/cases/compiler/badArrayIndex.ts (2 errors) ====
var results = number[];
~~~~~~
!!! error TS2552: Cannot find name 'number'. Did you mean 'Number'?
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
~
!!! error TS1109: Expression expected.
8 changes: 4 additions & 4 deletions tests/baselines/reference/bases.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
tests/cases/compiler/bases.ts(7,14): error TS2339: Property 'y' does not exist on type 'B'.
tests/cases/compiler/bases.ts(7,15): error TS1005: ';' expected.
tests/cases/compiler/bases.ts(7,17): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
tests/cases/compiler/bases.ts(7,17): error TS2693: 'any' only refers to a type, but is being used as a value here.
tests/cases/compiler/bases.ts(11,7): error TS2420: Class 'C' incorrectly implements interface 'I'.
Property 'x' is missing in type 'C'.
tests/cases/compiler/bases.ts(12,5): error TS2377: Constructors for derived classes must contain a 'super' call.
tests/cases/compiler/bases.ts(13,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
tests/cases/compiler/bases.ts(13,14): error TS2339: Property 'x' does not exist on type 'C'.
tests/cases/compiler/bases.ts(13,15): error TS1005: ';' expected.
tests/cases/compiler/bases.ts(13,17): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
tests/cases/compiler/bases.ts(13,17): error TS2693: 'any' only refers to a type, but is being used as a value here.
tests/cases/compiler/bases.ts(17,9): error TS2339: Property 'x' does not exist on type 'C'.
tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.

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

Expand All @@ -44,7 +44,7 @@ tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist o
~
!!! error TS1005: ';' expected.
~~~
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2693: 'any' only refers to a type, but is being used as a value here.


==== tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts (1 errors) ====
var test: any[] = new any[1];
~~~
!!! error TS2552: Cannot find name 'any'. Did you mean 'NaN'?
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
12 changes: 6 additions & 6 deletions tests/baselines/reference/classExtendingPrimitive.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(3,17): error TS2552: Cannot find name 'number'. Did you mean 'Number'?
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(4,18): error TS2552: Cannot find name 'string'. Did you mean 'String'?
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(5,18): error TS2552: Cannot find name 'boolean'. Did you mean 'Boolean'?
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.
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.
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.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(6,18): error TS2304: Cannot find name 'Void'.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(7,19): error TS1109: Expression expected.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(8,18): error TS2304: Cannot find name 'Null'.
Expand All @@ -14,13 +14,13 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla

class C extends number { }
~~~~~~
!!! error TS2552: Cannot find name 'number'. Did you mean 'Number'?
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
class C2 extends string { }
~~~~~~
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
class C3 extends boolean { }
~~~~~~~
!!! error TS2552: Cannot find name 'boolean'. Did you mean 'Boolean'?
!!! error TS2693: 'boolean' only refers to a type, but is being used as a value here.
class C4 extends Void { }
~~~~
!!! error TS2304: Cannot find name 'Void'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(4,17): error TS2689: Cannot extend an interface 'I'. Did you mean 'implements'?
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,18): error TS2507: Type '{ foo: any; }' is not a constructor function type.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,25): error TS2552: Cannot find name 'string'. Did you mean 'String'?
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.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(6,31): error TS1005: ',' expected.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(8,18): error TS2507: Type '{ foo: string; }' is not a constructor function type.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType.ts(11,18): error TS2507: Type 'typeof M' is not a constructor function type.
Expand All @@ -20,7 +20,7 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla
~~~~~~~~~~~~~~~~
!!! error TS2507: Type '{ foo: any; }' is not a constructor function type.
~~~~~~
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
~
!!! error TS1005: ',' expected.
var x: { foo: string; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,18): error TS2507: Type '{ foo: any; }' is not a constructor function type.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,25): error TS2552: Cannot find name 'string'. Did you mean 'String'?
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.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(1,31): error TS1005: ',' expected.
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsEveryObjectType2.ts(3,18): error TS2507: Type 'undefined[]' is not a constructor function type.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,11): error TS1146: D
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,12): error TS1005: '=' expected.
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,14): error TS2304: Cannot find name 'name'.
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,18): error TS1005: ']' expected.
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,19): error TS2552: Cannot find name 'string'. Did you mean 'String'?
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,19): error TS2693: 'string' only refers to a type, but is being used as a value here.
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,25): error TS1005: ',' expected.
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,26): error TS1136: Property assignment expected.
tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,27): error TS2304: Cannot find name 'VariableDeclaration'.
Expand All @@ -20,7 +20,7 @@ tests/cases/compiler/classMemberWithMissingIdentifier2.ts(2,27): error TS2304: C
~
!!! error TS1005: ']' expected.
~~~~~~
!!! error TS2552: Cannot find name 'string'. Did you mean 'String'?
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
~
!!! error TS1005: ',' expected.
~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/complicatedPrivacy.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/complicatedPrivacy.ts(11,24): error TS1054: A 'get' accessor cannot have parameters.
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.
tests/cases/compiler/complicatedPrivacy.ts(35,6): error TS2552: Cannot find name 'number'. Did you mean 'Number'?
tests/cases/compiler/complicatedPrivacy.ts(35,6): error TS2693: 'number' only refers to a type, but is being used as a value here.
tests/cases/compiler/complicatedPrivacy.ts(73,55): error TS2694: Namespace 'mglo5' has no exported member 'i6'.


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

Expand Down
Loading

0 comments on commit 2a7398b

Please sign in to comment.