541e553163
Error message improvement for unexpected tokens in the following situations: * A word was parsed that seems to have a low edit distance from a known common keyword * A word was parsed that seems to be a known common keyword and a name _without_ a space in-between * Parsing in a particular type of node (mostly a class property declaration) got a different word or token than expected ___ * Specific diagnostic suggestions for unexpected keywords or identifier * Don't reach into there, that's not allowed * Improved error when there is already an initializer * Specific module error message for invalid template literal strings * Skip 'unexpected keyword or identifier' diagnostics for declare nodes * Improve error for function calls in type positions * Switch class properties to old diagnostic * Corrected errors in class members and reused existing textToKeywordObj map * Corrected more baselines from the merge * Update src/compiler/parser.ts Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com> * Mostly addressed feedback * Clarified function call type message * Split up and clarified parsing vs error functions * Swap interface name complaints back, and skip new errors on unknown (invalid) tokens * Used tokenToString, not a raw semicolon * Inline getExpressionText helper * Remove remarks in src/compiler/parser.ts Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
358 lines
20 KiB
Plaintext
358 lines
20 KiB
Plaintext
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(1,7): error TS2300: Duplicate identifier 'A'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(14,5): error TS2322: Type 'string' is not assignable to type 'boolean'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(17,55): error TS2749: 'x' refers to a value, but is being used as a type here. Did you mean 'typeof x'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(17,57): error TS1144: '{' or ';' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(17,60): error TS1434: Unexpected keyword or identifier.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(21,33): error TS2749: 'x' refers to a value, but is being used as a type here. Did you mean 'typeof x'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(25,33): error TS1225: Cannot find parameter 'x'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(29,10): error TS2391: Function implementation is missing or not immediately following the declaration.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(30,5): error TS1131: Property or signature expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(31,1): error TS1128: Declaration or statement expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(33,38): error TS1225: Cannot find parameter 'x'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(37,51): error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
Property 'propA' is missing in type 'B' but required in type 'A'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(41,56): error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
Type 'number' is not assignable to type 'string'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(45,56): error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
Type 'T[]' is not assignable to type 'string'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(59,7): error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(64,7): error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(69,7): error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(74,46): error TS2345: Argument of type '(p1: any) => p1 is C' is not assignable to parameter of type '(p1: any) => p1 is B'.
|
||
Type predicate 'p1 is C' is not assignable to 'p1 is B'.
|
||
Property 'propB' is missing in type 'C' but required in type 'B'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(78,1): error TS2322: Type '(p1: any, p2: any) => boolean' is not assignable to type '(p1: any, p2: any) => p1 is A'.
|
||
Signature '(p1: any, p2: any): boolean' must be a type predicate.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(84,1): error TS2322: Type '(p1: any, p2: any) => p2 is A' is not assignable to type '(p1: any, p2: any) => p1 is A'.
|
||
Type predicate 'p2 is A' is not assignable to 'p1 is A'.
|
||
Parameter 'p2' is not in the same position as parameter 'p1'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(90,1): error TS2322: Type '(p1: any, p2: any, p3: any) => p1 is A' is not assignable to type '(p1: any, p2: any) => p1 is A'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(95,9): error TS2749: 'b' refers to a value, but is being used as a type here. Did you mean 'typeof b'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(95,11): error TS1005: ',' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(95,14): error TS1005: ',' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(95,14): error TS2300: Duplicate identifier 'A'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(96,16): error TS2749: 'b' refers to a value, but is being used as a type here. Did you mean 'typeof b'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(96,18): error TS1005: ',' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(96,21): error TS1005: ',' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(97,20): error TS2749: 'b' refers to a value, but is being used as a type here. Did you mean 'typeof b'?
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(97,22): error TS1144: '{' or ';' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(97,25): error TS1434: Unexpected keyword or identifier.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(103,25): error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(104,9): error TS2322: Type 'boolean' is not assignable to type 'D'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(104,9): error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(106,20): error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(109,20): error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(110,9): error TS2408: Setters cannot return a value.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(115,18): error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(119,22): error TS2304: Cannot find name 'p1'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(119,25): error TS1005: ';' expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(120,1): error TS1128: Declaration or statement expected.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(123,20): error TS1229: A type predicate cannot reference a rest parameter.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(128,34): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(132,34): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(136,39): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(152,68): error TS2344: Type 'T | "d"' does not satisfy the constraint 'Keys'.
|
||
Type '"d"' is not assignable to type 'Keys'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(159,31): error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
|
||
Types of property ''a'' are incompatible.
|
||
Type 'number' is not assignable to type 'string'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(162,31): error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(163,35): error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(164,51): error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(165,51): error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(166,45): error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
Type 'NeedsFoo<number>' is not assignable to type 'number'.
|
||
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(166,54): error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
|
||
|
||
|
||
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (53 errors) ====
|
||
class A {
|
||
~
|
||
!!! error TS2300: Duplicate identifier 'A'.
|
||
propA: number;
|
||
}
|
||
|
||
class B {
|
||
propB: number;
|
||
}
|
||
|
||
class C extends A {
|
||
propC: number;
|
||
}
|
||
|
||
function hasANonBooleanReturnStatement(x): x is A {
|
||
return '';
|
||
~~~~~~~~~~
|
||
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
|
||
}
|
||
|
||
function hasTypeGuardTypeInsideTypeGuardType(x): x is x is A {
|
||
~
|
||
!!! error TS2749: 'x' refers to a value, but is being used as a type here. Did you mean 'typeof x'?
|
||
~~
|
||
!!! error TS1144: '{' or ';' expected.
|
||
~
|
||
!!! error TS1434: Unexpected keyword or identifier.
|
||
return true;
|
||
}
|
||
|
||
function hasMissingIsKeyword(): x {
|
||
~
|
||
!!! error TS2749: 'x' refers to a value, but is being used as a type here. Did you mean 'typeof x'?
|
||
return true;
|
||
}
|
||
|
||
function hasMissingParameter(): x is A {
|
||
~
|
||
!!! error TS1225: Cannot find parameter 'x'.
|
||
return true;
|
||
}
|
||
|
||
function hasMissingTypeInTypeGuardType(x): x is {
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
|
||
return true;
|
||
~~~~~~
|
||
!!! error TS1131: Property or signature expected.
|
||
}
|
||
~
|
||
!!! error TS1128: Declaration or statement expected.
|
||
|
||
function hasNonMatchingParameter(y): x is A {
|
||
~
|
||
!!! error TS1225: Cannot find parameter 'x'.
|
||
return true;
|
||
}
|
||
|
||
function hasNonMatchingParameterType1(x: A): x is B {
|
||
~
|
||
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
!!! error TS2677: Property 'propA' is missing in type 'B' but required in type 'A'.
|
||
!!! related TS2728 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts:2:5: 'propA' is declared here.
|
||
return true;
|
||
}
|
||
|
||
function hasNonMatchingParameterType2(x: string): x is number {
|
||
~~~~~~
|
||
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
!!! error TS2677: Type 'number' is not assignable to type 'string'.
|
||
return true;
|
||
}
|
||
|
||
function hasNonMathcingGenericType<T>(a: string): a is T[] {
|
||
~~~
|
||
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
!!! error TS2677: Type 'T[]' is not assignable to type 'string'.
|
||
return true;
|
||
}
|
||
|
||
let a: A;
|
||
let b: B;
|
||
|
||
declare function isB(p1): p1 is B;
|
||
declare function isC(p1): p1 is C;
|
||
declare function funA(p1: any, p2: any): p1 is B;
|
||
declare function hasNoTypeGuard(x);
|
||
|
||
// Passed argument is not the same as the one being guarded.
|
||
if (isB(b)) {
|
||
a.propB;
|
||
~~~~~
|
||
!!! error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
|
||
!!! related TS2728 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts:2:5: 'propA' is declared here.
|
||
}
|
||
|
||
// Parameter index and argument index for the type guard target is not matching.
|
||
if (funA(0, a)) {
|
||
a.propB; // Error
|
||
~~~~~
|
||
!!! error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
|
||
!!! related TS2728 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts:2:5: 'propA' is declared here.
|
||
}
|
||
|
||
// No type guard in if statement
|
||
if (hasNoTypeGuard(a)) {
|
||
a.propB;
|
||
~~~~~
|
||
!!! error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
|
||
!!! related TS2728 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts:2:5: 'propA' is declared here.
|
||
}
|
||
|
||
// Type predicate type is not assignable
|
||
declare function acceptingDifferentSignatureTypeGuardFunction(p1: (p1) => p1 is B);
|
||
acceptingDifferentSignatureTypeGuardFunction(isC);
|
||
~~~
|
||
!!! error TS2345: Argument of type '(p1: any) => p1 is C' is not assignable to parameter of type '(p1: any) => p1 is B'.
|
||
!!! error TS2345: Type predicate 'p1 is C' is not assignable to 'p1 is B'.
|
||
!!! error TS2345: Property 'propB' is missing in type 'C' but required in type 'B'.
|
||
!!! related TS2728 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts:6:5: 'propB' is declared here.
|
||
|
||
// Boolean not assignable to type guard
|
||
var assign1: (p1, p2) => p1 is A;
|
||
assign1 = function(p1, p2): boolean {
|
||
~~~~~~~
|
||
!!! error TS2322: Type '(p1: any, p2: any) => boolean' is not assignable to type '(p1: any, p2: any) => p1 is A'.
|
||
!!! error TS2322: Signature '(p1: any, p2: any): boolean' must be a type predicate.
|
||
return true;
|
||
};
|
||
|
||
// Must have matching parameter index
|
||
var assign2: (p1, p2) => p1 is A;
|
||
assign2 = function(p1, p2): p2 is A {
|
||
~~~~~~~
|
||
!!! error TS2322: Type '(p1: any, p2: any) => p2 is A' is not assignable to type '(p1: any, p2: any) => p1 is A'.
|
||
!!! error TS2322: Type predicate 'p2 is A' is not assignable to 'p1 is A'.
|
||
!!! error TS2322: Parameter 'p2' is not in the same position as parameter 'p1'.
|
||
return true;
|
||
};
|
||
|
||
// No matching signature
|
||
var assign3: (p1, p2) => p1 is A;
|
||
assign3 = function(p1, p2, p3): p1 is A {
|
||
~~~~~~~
|
||
!!! error TS2322: Type '(p1: any, p2: any, p3: any) => p1 is A' is not assignable to type '(p1: any, p2: any) => p1 is A'.
|
||
return true;
|
||
};
|
||
|
||
// Type predicates in non-return type positions
|
||
var b1: b is A;
|
||
~
|
||
!!! error TS2749: 'b' refers to a value, but is being used as a type here. Did you mean 'typeof b'?
|
||
~~
|
||
!!! error TS1005: ',' expected.
|
||
~
|
||
!!! error TS1005: ',' expected.
|
||
~
|
||
!!! error TS2300: Duplicate identifier 'A'.
|
||
function b2(a: b is A) {};
|
||
~
|
||
!!! error TS2749: 'b' refers to a value, but is being used as a type here. Did you mean 'typeof b'?
|
||
~~
|
||
!!! error TS1005: ',' expected.
|
||
~
|
||
!!! error TS1005: ',' expected.
|
||
function b3(): A | b is A {
|
||
~
|
||
!!! error TS2749: 'b' refers to a value, but is being used as a type here. Did you mean 'typeof b'?
|
||
~~
|
||
!!! error TS1144: '{' or ';' expected.
|
||
~
|
||
!!! error TS1434: Unexpected keyword or identifier.
|
||
return true;
|
||
};
|
||
|
||
// Non-compatiable type predicate positions for signature declarations
|
||
class D {
|
||
constructor(p1: A): p1 is C {
|
||
~~~~~~~
|
||
!!! error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
return true;
|
||
~~~~~~~~~~~~
|
||
!!! error TS2322: Type 'boolean' is not assignable to type 'D'.
|
||
~~~~~~~~~~~~
|
||
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
|
||
}
|
||
get m1(p1: A): p1 is C {
|
||
~~~~~~~
|
||
!!! error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
return true;
|
||
}
|
||
set m2(p1: A): p1 is C {
|
||
~~~~~~~
|
||
!!! error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
return true;
|
||
~~~~~~~~~~~~
|
||
!!! error TS2408: Setters cannot return a value.
|
||
}
|
||
}
|
||
|
||
interface I1 {
|
||
new (p1: A): p1 is C;
|
||
~~~~~~~
|
||
!!! error TS1228: A type predicate is only allowed in return type position for functions and methods.
|
||
}
|
||
|
||
interface I2 {
|
||
[index: number]: p1 is C;
|
||
~~
|
||
!!! error TS2304: Cannot find name 'p1'.
|
||
~~
|
||
!!! error TS1005: ';' expected.
|
||
}
|
||
~
|
||
!!! error TS1128: Declaration or statement expected.
|
||
|
||
// Reference to rest parameter
|
||
function b4(...a): a is A {
|
||
~
|
||
!!! error TS1229: A type predicate cannot reference a rest parameter.
|
||
return true;
|
||
}
|
||
|
||
// Reference to binding pattern
|
||
function b5({a, b, p1}, p2, p3): p1 is A {
|
||
~~
|
||
!!! error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
|
||
return true;
|
||
}
|
||
|
||
function b6([a, b, p1], p2, p3): p1 is A {
|
||
~~
|
||
!!! error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
|
||
return true;
|
||
}
|
||
|
||
function b7({a, b, c: {p1}}, p2, p3): p1 is A {
|
||
~~
|
||
!!! error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
|
||
return true;
|
||
}
|
||
|
||
// Should not crash the compiler
|
||
var x: A;
|
||
if (hasMissingParameter()) {
|
||
x.propA;
|
||
}
|
||
|
||
// repro #17297
|
||
|
||
type Keys = 'a'|'b'|'c'
|
||
type KeySet<T extends Keys> = { [k in T]: true }
|
||
|
||
// expected an error, since Keys doesn't have a 'd'
|
||
declare function hasKey<T extends Keys>(x: KeySet<T>): x is KeySet<T|'d'>;
|
||
~~~~~
|
||
!!! error TS2344: Type 'T | "d"' does not satisfy the constraint 'Keys'.
|
||
!!! error TS2344: Type '"d"' is not assignable to type 'Keys'.
|
||
|
||
type Foo = { 'a': string; }
|
||
type Bar = { 'a': number; }
|
||
|
||
interface NeedsFoo<T extends Foo> {
|
||
foo: T;
|
||
isFoo(): this is NeedsFoo<Bar>; // should error
|
||
~~~
|
||
!!! error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
|
||
!!! error TS2344: Types of property ''a'' are incompatible.
|
||
!!! error TS2344: Type 'number' is not assignable to type 'string'.
|
||
};
|
||
|
||
declare var anError: NeedsFoo<Bar>; // error, as expected
|
||
~~~
|
||
!!! error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
|
||
declare var alsoAnError: NeedsFoo<number>; // also error, as expected
|
||
~~~~~~
|
||
!!! error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
|
||
declare function newError1(x: any): x is NeedsFoo<Bar>; // should error
|
||
~~~
|
||
!!! error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
|
||
declare function newError2(x: any): x is NeedsFoo<number>; // should error
|
||
~~~~~~
|
||
!!! error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
|
||
declare function newError3(x: number): x is NeedsFoo<number>; // should error
|
||
~~~~~~~~~~~~~~~~
|
||
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
|
||
!!! error TS2677: Type 'NeedsFoo<number>' is not assignable to type 'number'.
|
||
~~~~~~
|
||
!!! error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
|
||
|