2014-10-10 23:41:14 +02:00
|
|
|
=== tests/cases/compiler/arrayLiteralContextualType.ts ===
|
|
|
|
interface IAnimal {
|
|
|
|
>IAnimal : IAnimal
|
|
|
|
|
|
|
|
name: string;
|
|
|
|
>name : string
|
|
|
|
}
|
|
|
|
|
|
|
|
class Giraffe {
|
|
|
|
>Giraffe : Giraffe
|
|
|
|
|
|
|
|
name = "Giraffe";
|
|
|
|
>name : string
|
|
|
|
|
|
|
|
neckLength = "3m";
|
|
|
|
>neckLength : string
|
|
|
|
}
|
|
|
|
|
|
|
|
class Elephant {
|
|
|
|
>Elephant : Elephant
|
|
|
|
|
|
|
|
name = "Elephant";
|
|
|
|
>name : string
|
|
|
|
|
|
|
|
trunkDiameter = "20cm";
|
|
|
|
>trunkDiameter : string
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(animals: IAnimal[]) { }
|
|
|
|
>foo : (animals: IAnimal[]) => void
|
|
|
|
>animals : IAnimal[]
|
|
|
|
>IAnimal : IAnimal
|
|
|
|
|
|
|
|
function bar(animals: { [n: number]: IAnimal }) { }
|
2014-12-06 01:58:49 +01:00
|
|
|
>bar : (animals: { [n: number]: IAnimal; }) => void
|
|
|
|
>animals : { [n: number]: IAnimal; }
|
2014-10-10 23:41:14 +02:00
|
|
|
>n : number
|
|
|
|
>IAnimal : IAnimal
|
|
|
|
|
|
|
|
foo([
|
|
|
|
>foo([ new Giraffe(), new Elephant()]) : void
|
|
|
|
>foo : (animals: IAnimal[]) => void
|
2014-10-18 01:53:18 +02:00
|
|
|
>[ new Giraffe(), new Elephant()] : (Giraffe | Elephant)[]
|
2014-10-10 23:41:14 +02:00
|
|
|
|
|
|
|
new Giraffe(),
|
|
|
|
>new Giraffe() : Giraffe
|
|
|
|
>Giraffe : typeof Giraffe
|
|
|
|
|
|
|
|
new Elephant()
|
|
|
|
>new Elephant() : Elephant
|
|
|
|
>Elephant : typeof Elephant
|
|
|
|
|
|
|
|
]); // Legal because of the contextual type IAnimal provided by the parameter
|
|
|
|
bar([
|
|
|
|
>bar([ new Giraffe(), new Elephant()]) : void
|
2014-12-06 01:58:49 +01:00
|
|
|
>bar : (animals: { [n: number]: IAnimal; }) => void
|
2014-10-18 01:53:18 +02:00
|
|
|
>[ new Giraffe(), new Elephant()] : (Giraffe | Elephant)[]
|
2014-10-10 23:41:14 +02:00
|
|
|
|
|
|
|
new Giraffe(),
|
|
|
|
>new Giraffe() : Giraffe
|
|
|
|
>Giraffe : typeof Giraffe
|
|
|
|
|
|
|
|
new Elephant()
|
|
|
|
>new Elephant() : Elephant
|
|
|
|
>Elephant : typeof Elephant
|
|
|
|
|
|
|
|
]); // Legal because of the contextual type IAnimal provided by the parameter
|
|
|
|
|
|
|
|
var arr = [new Giraffe(), new Elephant()];
|
2014-10-18 01:53:18 +02:00
|
|
|
>arr : (Giraffe | Elephant)[]
|
|
|
|
>[new Giraffe(), new Elephant()] : (Giraffe | Elephant)[]
|
2014-10-10 23:41:14 +02:00
|
|
|
>new Giraffe() : Giraffe
|
|
|
|
>Giraffe : typeof Giraffe
|
|
|
|
>new Elephant() : Elephant
|
|
|
|
>Elephant : typeof Elephant
|
|
|
|
|
|
|
|
foo(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]
|
|
|
|
>foo(arr) : void
|
|
|
|
>foo : (animals: IAnimal[]) => void
|
2014-10-18 01:53:18 +02:00
|
|
|
>arr : (Giraffe | Elephant)[]
|
2014-10-10 23:41:14 +02:00
|
|
|
|
|
|
|
bar(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]
|
|
|
|
>bar(arr) : void
|
2014-12-06 01:58:49 +01:00
|
|
|
>bar : (animals: { [n: number]: IAnimal; }) => void
|
2014-10-18 01:53:18 +02:00
|
|
|
>arr : (Giraffe | Elephant)[]
|
2014-10-10 23:41:14 +02:00
|
|
|
|