2014-07-13 01:04:16 +02:00
|
|
|
==== tests/cases/compiler/arrayLiteralContextualType.ts (2 errors) ====
|
|
|
|
interface IAnimal {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Giraffe {
|
|
|
|
name = "Giraffe";
|
|
|
|
neckLength = "3m";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Elephant {
|
|
|
|
name = "Elephant";
|
|
|
|
trunkDiameter = "20cm";
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(animals: IAnimal[]) { }
|
|
|
|
function bar(animals: { [n: number]: IAnimal }) { }
|
|
|
|
|
|
|
|
foo([
|
|
|
|
new Giraffe(),
|
|
|
|
new Elephant()
|
|
|
|
]); // Legal because of the contextual type IAnimal provided by the parameter
|
|
|
|
bar([
|
|
|
|
new Giraffe(),
|
|
|
|
new Elephant()
|
|
|
|
]); // Legal because of the contextual type IAnimal provided by the parameter
|
|
|
|
|
|
|
|
var arr = [new Giraffe(), new Elephant()];
|
|
|
|
foo(arr); // Error because of no contextual type
|
2014-07-25 02:00:03 +02:00
|
|
|
~~~
|
2014-09-12 00:19:57 +02:00
|
|
|
!!! error TS2345: Argument of type '{}[]' is not assignable to parameter of type 'IAnimal[]'.
|
|
|
|
!!! error TS2345: Type '{}' is not assignable to type 'IAnimal'.
|
2014-07-13 01:04:16 +02:00
|
|
|
bar(arr); // Error because of no contextual type
|
2014-07-25 02:00:03 +02:00
|
|
|
~~~
|
2014-09-12 00:19:57 +02:00
|
|
|
!!! error TS2345: Argument of type '{}[]' is not assignable to parameter of type '{ [x: number]: IAnimal; }'.
|