35 lines
No EOL
1.1 KiB
Text
35 lines
No EOL
1.1 KiB
Text
==== 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
|
|
~~~
|
|
!!! Argument of type '{}[]' is not assignable to parameter of type 'IAnimal[]'.
|
|
!!! Type '{}' is not assignable to type 'IAnimal'.
|
|
bar(arr); // Error because of no contextual type
|
|
~~~
|
|
!!! Argument of type '{}[]' is not assignable to parameter of type '{ [x: number]: IAnimal; }'. |