From 8e7be9ad98808cc5eb5946fb1a2644de466ce2b9 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 5 Sep 2014 17:09:00 -0700 Subject: [PATCH] Properly report errors for failed type assertions --- src/compiler/checker.ts | 4 ++-- tests/baselines/reference/arrayCast.errors.txt | 5 +++-- .../baselines/reference/contextualTyping39.errors.txt | 3 ++- .../baselines/reference/contextualTyping41.errors.txt | 3 ++- .../defaultArgsInFunctionExpressions.errors.txt | 4 ++-- tests/baselines/reference/fuzzy.errors.txt | 3 ++- .../reference/genericTypeAssertions1.errors.txt | 5 +++-- .../reference/genericTypeAssertions2.errors.txt | 3 ++- .../reference/genericTypeAssertions4.errors.txt | 4 ++-- .../reference/genericTypeAssertions5.errors.txt | 4 ++-- .../reference/genericTypeAssertions6.errors.txt | 6 +++--- tests/baselines/reference/intTypeCheck.errors.txt | 2 +- tests/baselines/reference/literals-negative.errors.txt | 2 +- tests/baselines/reference/typeAssertions.errors.txt | 10 ++++++---- 14 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 151d6d7e87b..4256ef7cefc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4330,8 +4330,8 @@ module ts { var targetType = getTypeFromTypeNode(node.type); if (fullTypeCheck && targetType !== unknownType) { var widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(exprType, targetType) || isTypeAssignableTo(targetType, widenedType))) { - checkTypeAssignableTo(targetType, widenedType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); + if (!(isTypeAssignableTo(targetType, widenedType))) { + checkTypeAssignableTo(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } return targetType; diff --git a/tests/baselines/reference/arrayCast.errors.txt b/tests/baselines/reference/arrayCast.errors.txt index f081e0008d9..509ee2110b7 100644 --- a/tests/baselines/reference/arrayCast.errors.txt +++ b/tests/baselines/reference/arrayCast.errors.txt @@ -3,8 +3,9 @@ // has type { foo: string }[], which is not assignable to { id: number }[]. <{ id: number; }[]>[{ foo: "s" }]; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! Neither type '{ id: number; }[]' nor type '{ foo: string; }[]' is assignable to the other: -!!! Type '{ id: number; }' is not assignable to type '{ foo: string; }'. +!!! Neither type '{ foo: string; }[]' nor type '{ id: number; }[]' is assignable to the other: +!!! Type '{ foo: string; }' is not assignable to type '{ id: number; }': +!!! Property 'id' is missing in type '{ foo: string; }'. // Should succeed, as the {} element causes the type of the array to be {}[] <{ id: number; }[]>[{ foo: "s" }, {}]; \ No newline at end of file diff --git a/tests/baselines/reference/contextualTyping39.errors.txt b/tests/baselines/reference/contextualTyping39.errors.txt index c0907b8fe7b..10be2415329 100644 --- a/tests/baselines/reference/contextualTyping39.errors.txt +++ b/tests/baselines/reference/contextualTyping39.errors.txt @@ -1,4 +1,5 @@ ==== tests/cases/compiler/contextualTyping39.ts (1 errors) ==== var foo = <{ (): number; }> function() { return "err"; }; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! Neither type '() => number' nor type '() => string' is assignable to the other. \ No newline at end of file +!!! Neither type '() => string' nor type '() => number' is assignable to the other: +!!! Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/contextualTyping41.errors.txt b/tests/baselines/reference/contextualTyping41.errors.txt index d74e45f8a08..6e95b0092ca 100644 --- a/tests/baselines/reference/contextualTyping41.errors.txt +++ b/tests/baselines/reference/contextualTyping41.errors.txt @@ -1,4 +1,5 @@ ==== tests/cases/compiler/contextualTyping41.ts (1 errors) ==== var foo = <{():number; (i:number):number; }> (function(){return "err";}); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! Neither type '{ (): number; (i: number): number; }' nor type '() => string' is assignable to the other. \ No newline at end of file +!!! Neither type '() => string' nor type '{ (): number; (i: number): number; }' is assignable to the other: +!!! Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt b/tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt index b34c4902000..7fb00da2b40 100644 --- a/tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt +++ b/tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt @@ -22,7 +22,7 @@ // Contextually type the default arg with the type annotation var f3 = function (a: (s: string) => any = (s) => s) { }; ~~~~~~~~~ -!!! Neither type 'number' nor type 'string' is assignable to the other. +!!! Neither type 'string' nor type 'number' is assignable to the other. // Type check using the function's contextual type var f4: (a: number) => void = function (a = "") { }; @@ -32,7 +32,7 @@ // Contextually type the default arg using the function's contextual type var f5: (a: (s: string) => any) => void = function (a = s => s) { }; ~~~~~~~~~ -!!! Neither type 'number' nor type 'string' is assignable to the other. +!!! Neither type 'string' nor type 'number' is assignable to the other. // Instantiated module module T { } diff --git a/tests/baselines/reference/fuzzy.errors.txt b/tests/baselines/reference/fuzzy.errors.txt index dfe667b5be9..36124da5cea 100644 --- a/tests/baselines/reference/fuzzy.errors.txt +++ b/tests/baselines/reference/fuzzy.errors.txt @@ -32,7 +32,8 @@ worksToo():R { return ({ oneI: this }); ~~~~~~~~~~~~~~~~~~~ -!!! Neither type 'R' nor type '{ oneI: C; }' is assignable to the other. +!!! Neither type '{ oneI: C; }' nor type 'R' is assignable to the other: +!!! Property 'anything' is missing in type '{ oneI: C; }'. } } } diff --git a/tests/baselines/reference/genericTypeAssertions1.errors.txt b/tests/baselines/reference/genericTypeAssertions1.errors.txt index 4a9b9207575..7ab9e056611 100644 --- a/tests/baselines/reference/genericTypeAssertions1.errors.txt +++ b/tests/baselines/reference/genericTypeAssertions1.errors.txt @@ -10,5 +10,6 @@ !!! Type 'A>' is not assignable to type 'A': !!! Type 'A' is not assignable to type 'number'. ~~~~~~~~~~~~~~~~~ -!!! Neither type 'A>' nor type 'A' is assignable to the other: -!!! Type 'A' is not assignable to type 'number'. \ No newline at end of file +!!! Neither type 'A' nor type 'A>' is assignable to the other: +!!! Type 'number' is not assignable to type 'A': +!!! Property 'foo' is missing in type 'Number'. \ No newline at end of file diff --git a/tests/baselines/reference/genericTypeAssertions2.errors.txt b/tests/baselines/reference/genericTypeAssertions2.errors.txt index e2f874b5983..886031edaaf 100644 --- a/tests/baselines/reference/genericTypeAssertions2.errors.txt +++ b/tests/baselines/reference/genericTypeAssertions2.errors.txt @@ -22,4 +22,5 @@ var r4: A = >new A(); var r5: A = >[]; // error ~~~~~~~~~~~~~ -!!! Neither type 'A' nor type 'any[]' is assignable to the other. \ No newline at end of file +!!! Neither type 'undefined[]' nor type 'A' is assignable to the other: +!!! Property 'foo' is missing in type 'undefined[]'. \ No newline at end of file diff --git a/tests/baselines/reference/genericTypeAssertions4.errors.txt b/tests/baselines/reference/genericTypeAssertions4.errors.txt index 13a4e2929b3..c109b6988e9 100644 --- a/tests/baselines/reference/genericTypeAssertions4.errors.txt +++ b/tests/baselines/reference/genericTypeAssertions4.errors.txt @@ -29,8 +29,8 @@ y = a; y = b; // error: cannot convert B to T ~~~~ -!!! Neither type 'T' nor type 'B' is assignable to the other. +!!! Neither type 'B' nor type 'T' is assignable to the other. y = c; // error: cannot convert C to T ~~~~ -!!! Neither type 'T' nor type 'C' is assignable to the other. +!!! Neither type 'C' nor type 'T' is assignable to the other. } \ No newline at end of file diff --git a/tests/baselines/reference/genericTypeAssertions5.errors.txt b/tests/baselines/reference/genericTypeAssertions5.errors.txt index 7b25b898213..7fe23527273 100644 --- a/tests/baselines/reference/genericTypeAssertions5.errors.txt +++ b/tests/baselines/reference/genericTypeAssertions5.errors.txt @@ -29,8 +29,8 @@ y = a; y = b; // error: cannot convert B to T ~~~~ -!!! Neither type 'T' nor type 'B' is assignable to the other. +!!! Neither type 'B' nor type 'T' is assignable to the other. y = c; // error: cannot convert C to T ~~~~ -!!! Neither type 'T' nor type 'C' is assignable to the other. +!!! Neither type 'C' nor type 'T' is assignable to the other. } \ No newline at end of file diff --git a/tests/baselines/reference/genericTypeAssertions6.errors.txt b/tests/baselines/reference/genericTypeAssertions6.errors.txt index abc78833071..99d7f6b8509 100644 --- a/tests/baselines/reference/genericTypeAssertions6.errors.txt +++ b/tests/baselines/reference/genericTypeAssertions6.errors.txt @@ -8,10 +8,10 @@ f(x: T, y: U) { x = y; ~~~~ -!!! Neither type 'T' nor type 'U' is assignable to the other. +!!! Neither type 'U' nor type 'T' is assignable to the other. y = x; ~~~~ -!!! Neither type 'U' nor type 'T' is assignable to the other. +!!! Neither type 'T' nor type 'U' is assignable to the other. } } @@ -23,7 +23,7 @@ var d = new Date(); var e = new Date(); ~~~~~~~~~~~~~~~~ -!!! Neither type 'T' nor type 'U' is assignable to the other. +!!! Neither type 'U' nor type 'T' is assignable to the other. } } diff --git a/tests/baselines/reference/intTypeCheck.errors.txt b/tests/baselines/reference/intTypeCheck.errors.txt index cb2075efbdc..29462bbf527 100644 --- a/tests/baselines/reference/intTypeCheck.errors.txt +++ b/tests/baselines/reference/intTypeCheck.errors.txt @@ -315,7 +315,7 @@ var obj69: i7 = new obj66; var obj70: i7 = new Base; ~~~~~~~~~~~~ -!!! Neither type 'i7' nor type 'Base' is assignable to the other. +!!! Neither type 'Base' nor type 'i7' is assignable to the other. var obj71: i7 = null; var obj72: i7 = function () { }; ~~~~~ diff --git a/tests/baselines/reference/literals-negative.errors.txt b/tests/baselines/reference/literals-negative.errors.txt index a7e7b08d46a..276afb5a81d 100644 --- a/tests/baselines/reference/literals-negative.errors.txt +++ b/tests/baselines/reference/literals-negative.errors.txt @@ -5,7 +5,7 @@ var s = (null); var b = (n); ~~~~~~~~~~~~ -!!! Neither type 'boolean' nor type 'number' is assignable to the other. +!!! Neither type 'number' nor type 'boolean' is assignable to the other. function isVoid() : void { } diff --git a/tests/baselines/reference/typeAssertions.errors.txt b/tests/baselines/reference/typeAssertions.errors.txt index 47f3a3c930c..abdbac6b4ac 100644 --- a/tests/baselines/reference/typeAssertions.errors.txt +++ b/tests/baselines/reference/typeAssertions.errors.txt @@ -33,20 +33,22 @@ someBase = someBase; someBase = someOther; // Error ~~~~~~~~~~~~~~~~~~~ -!!! Neither type 'SomeBase' nor type 'SomeOther' is assignable to the other. +!!! Neither type 'SomeOther' nor type 'SomeBase' is assignable to the other: +!!! Property 'p' is missing in type 'SomeOther'. someDerived = someDerived; someDerived = someBase; someDerived = someOther; // Error ~~~~~~~~~~~~~~~~~~~~~~ -!!! Neither type 'SomeDerived' nor type 'SomeOther' is assignable to the other. +!!! Neither type 'SomeOther' nor type 'SomeDerived' is assignable to the other: +!!! Property 'x' is missing in type 'SomeOther'. someOther = someDerived; // Error ~~~~~~~~~~~~~~~~~~~~~~ -!!! Neither type 'SomeOther' nor type 'SomeDerived' is assignable to the other. +!!! Neither type 'SomeDerived' nor type 'SomeOther' is assignable to the other. someOther = someBase; // Error ~~~~~~~~~~~~~~~~~~~ -!!! Neither type 'SomeOther' nor type 'SomeBase' is assignable to the other. +!!! Neither type 'SomeBase' nor type 'SomeOther' is assignable to the other. someOther = someOther;