Skip to content

Commit 2d7d491

Browse files
committed
Merge pull request #618 from Microsoft/typeAssertionErrors
Properly report errors for failed type assertions
2 parents 1eacacd + 8e7be9a commit 2d7d491

14 files changed

+33
-25
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4330,8 +4330,8 @@ module ts {
43304330
var targetType = getTypeFromTypeNode(node.type);
43314331
if (fullTypeCheck && targetType !== unknownType) {
43324332
var widenedType = getWidenedType(exprType);
4333-
if (!(isTypeAssignableTo(exprType, targetType) || isTypeAssignableTo(targetType, widenedType))) {
4334-
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);
4333+
if (!(isTypeAssignableTo(targetType, widenedType))) {
4334+
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);
43354335
}
43364336
}
43374337
return targetType;

tests/baselines/reference/arrayCast.errors.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// has type { foo: string }[], which is not assignable to { id: number }[].
44
<{ id: number; }[]>[{ foo: "s" }];
55
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6-
!!! Neither type '{ id: number; }[]' nor type '{ foo: string; }[]' is assignable to the other:
7-
!!! Type '{ id: number; }' is not assignable to type '{ foo: string; }'.
6+
!!! Neither type '{ foo: string; }[]' nor type '{ id: number; }[]' is assignable to the other:
7+
!!! Type '{ foo: string; }' is not assignable to type '{ id: number; }':
8+
!!! Property 'id' is missing in type '{ foo: string; }'.
89

910
// Should succeed, as the {} element causes the type of the array to be {}[]
1011
<{ id: number; }[]>[{ foo: "s" }, {}];
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
==== tests/cases/compiler/contextualTyping39.ts (1 errors) ====
22
var foo = <{ (): number; }> function() { return "err"; };
33
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4-
!!! Neither type '() => number' nor type '() => string' is assignable to the other.
4+
!!! Neither type '() => string' nor type '() => number' is assignable to the other:
5+
!!! Type 'string' is not assignable to type 'number'.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
==== tests/cases/compiler/contextualTyping41.ts (1 errors) ====
22
var foo = <{():number; (i:number):number; }> (function(){return "err";});
33
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4-
!!! Neither type '{ (): number; (i: number): number; }' nor type '() => string' is assignable to the other.
4+
!!! Neither type '() => string' nor type '{ (): number; (i: number): number; }' is assignable to the other:
5+
!!! Type 'string' is not assignable to type 'number'.

tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Contextually type the default arg with the type annotation
2323
var f3 = function (a: (s: string) => any = (s) => <number>s) { };
2424
~~~~~~~~~
25-
!!! Neither type 'number' nor type 'string' is assignable to the other.
25+
!!! Neither type 'string' nor type 'number' is assignable to the other.
2626

2727
// Type check using the function's contextual type
2828
var f4: (a: number) => void = function (a = "") { };
@@ -32,7 +32,7 @@
3232
// Contextually type the default arg using the function's contextual type
3333
var f5: (a: (s: string) => any) => void = function (a = s => <number>s) { };
3434
~~~~~~~~~
35-
!!! Neither type 'number' nor type 'string' is assignable to the other.
35+
!!! Neither type 'string' nor type 'number' is assignable to the other.
3636

3737
// Instantiated module
3838
module T { }

tests/baselines/reference/fuzzy.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
worksToo():R {
3333
return <R>({ oneI: this });
3434
~~~~~~~~~~~~~~~~~~~
35-
!!! Neither type 'R' nor type '{ oneI: C; }' is assignable to the other.
35+
!!! Neither type '{ oneI: C; }' nor type 'R' is assignable to the other:
36+
!!! Property 'anything' is missing in type '{ oneI: C; }'.
3637
}
3738
}
3839
}

tests/baselines/reference/genericTypeAssertions1.errors.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
!!! Type 'A<A<number>>' is not assignable to type 'A<number>':
1111
!!! Type 'A<number>' is not assignable to type 'number'.
1212
~~~~~~~~~~~~~~~~~
13-
!!! Neither type 'A<A<number>>' nor type 'A<number>' is assignable to the other:
14-
!!! Type 'A<number>' is not assignable to type 'number'.
13+
!!! Neither type 'A<number>' nor type 'A<A<number>>' is assignable to the other:
14+
!!! Type 'number' is not assignable to type 'A<number>':
15+
!!! Property 'foo' is missing in type 'Number'.

tests/baselines/reference/genericTypeAssertions2.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
var r4: A<number> = <A<number>>new A();
2323
var r5: A<number> = <A<number>>[]; // error
2424
~~~~~~~~~~~~~
25-
!!! Neither type 'A<number>' nor type 'any[]' is assignable to the other.
25+
!!! Neither type 'undefined[]' nor type 'A<number>' is assignable to the other:
26+
!!! Property 'foo' is missing in type 'undefined[]'.

tests/baselines/reference/genericTypeAssertions4.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
y = <T>a;
3030
y = <T>b; // error: cannot convert B to T
3131
~~~~
32-
!!! Neither type 'T' nor type 'B' is assignable to the other.
32+
!!! Neither type 'B' nor type 'T' is assignable to the other.
3333
y = <T>c; // error: cannot convert C to T
3434
~~~~
35-
!!! Neither type 'T' nor type 'C' is assignable to the other.
35+
!!! Neither type 'C' nor type 'T' is assignable to the other.
3636
}

tests/baselines/reference/genericTypeAssertions5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
y = <T>a;
3030
y = <T>b; // error: cannot convert B to T
3131
~~~~
32-
!!! Neither type 'T' nor type 'B' is assignable to the other.
32+
!!! Neither type 'B' nor type 'T' is assignable to the other.
3333
y = <T>c; // error: cannot convert C to T
3434
~~~~
35-
!!! Neither type 'T' nor type 'C' is assignable to the other.
35+
!!! Neither type 'C' nor type 'T' is assignable to the other.
3636
}

0 commit comments

Comments
 (0)