mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-20 13:45:34 -05:00
Properly report errors for failed type assertions
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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" }, {}];
|
||||
@@ -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.
|
||||
!!! Neither type '() => string' nor type '() => number' is assignable to the other:
|
||||
!!! Type 'string' is not assignable to type 'number'.
|
||||
@@ -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.
|
||||
!!! Neither type '() => string' nor type '{ (): number; (i: number): number; }' is assignable to the other:
|
||||
!!! Type 'string' is not assignable to type 'number'.
|
||||
@@ -22,7 +22,7 @@
|
||||
// Contextually type the default arg with the type annotation
|
||||
var f3 = function (a: (s: string) => any = (s) => <number>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 => <number>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 { }
|
||||
|
||||
@@ -32,7 +32,8 @@
|
||||
worksToo():R {
|
||||
return <R>({ 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; }'.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,5 +10,6 @@
|
||||
!!! Type 'A<A<number>>' is not assignable to type 'A<number>':
|
||||
!!! Type 'A<number>' is not assignable to type 'number'.
|
||||
~~~~~~~~~~~~~~~~~
|
||||
!!! Neither type 'A<A<number>>' nor type 'A<number>' is assignable to the other:
|
||||
!!! Type 'A<number>' is not assignable to type 'number'.
|
||||
!!! Neither type 'A<number>' nor type 'A<A<number>>' is assignable to the other:
|
||||
!!! Type 'number' is not assignable to type 'A<number>':
|
||||
!!! Property 'foo' is missing in type 'Number'.
|
||||
@@ -22,4 +22,5 @@
|
||||
var r4: A<number> = <A<number>>new A();
|
||||
var r5: A<number> = <A<number>>[]; // error
|
||||
~~~~~~~~~~~~~
|
||||
!!! Neither type 'A<number>' nor type 'any[]' is assignable to the other.
|
||||
!!! Neither type 'undefined[]' nor type 'A<number>' is assignable to the other:
|
||||
!!! Property 'foo' is missing in type 'undefined[]'.
|
||||
@@ -29,8 +29,8 @@
|
||||
y = <T>a;
|
||||
y = <T>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 = <T>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.
|
||||
}
|
||||
@@ -29,8 +29,8 @@
|
||||
y = <T>a;
|
||||
y = <T>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 = <T>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.
|
||||
}
|
||||
@@ -8,10 +8,10 @@
|
||||
f(x: T, y: U) {
|
||||
x = <T>y;
|
||||
~~~~
|
||||
!!! Neither type 'T' nor type 'U' is assignable to the other.
|
||||
!!! Neither type 'U' nor type 'T' is assignable to the other.
|
||||
y = <U>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 = <U>new Date();
|
||||
var e = <T><U>new Date();
|
||||
~~~~~~~~~~~~~~~~
|
||||
!!! Neither type 'T' nor type 'U' is assignable to the other.
|
||||
!!! Neither type 'U' nor type 'T' is assignable to the other.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@
|
||||
var obj69: i7 = new obj66;
|
||||
var obj70: i7 = <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 () { };
|
||||
~~~~~
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
var s = <string>(null);
|
||||
var b = <boolean>(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 { }
|
||||
|
||||
|
||||
@@ -33,20 +33,22 @@
|
||||
someBase = <SomeBase>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;
|
||||
someDerived = <SomeDerived>someBase;
|
||||
someDerived = <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 = <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 = <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>someOther;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user