1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
=== tests/cases/compiler/arrayLiteralContextualType.ts ===
interface IAnimal {
>IAnimal : IAnimal
name: string;
>name : string
}
class Giraffe {
>Giraffe : Giraffe
name = "Giraffe";
>name : string
>"Giraffe" : "Giraffe"
neckLength = "3m";
>neckLength : string
>"3m" : "3m"
}
class Elephant {
>Elephant : Elephant
name = "Elephant";
>name : string
>"Elephant" : "Elephant"
trunkDiameter = "20cm";
>trunkDiameter : string
>"20cm" : "20cm"
}
function foo(animals: IAnimal[]) { }
>foo : (animals: IAnimal[]) => void
>animals : IAnimal[]
>IAnimal : IAnimal
function bar(animals: { [n: number]: IAnimal }) { }
>bar : (animals: { [n: number]: IAnimal; }) => void
>animals : { [n: number]: IAnimal; }
>n : number
>IAnimal : IAnimal
foo([
>foo([ new Giraffe(), new Elephant()]) : void
>foo : (animals: IAnimal[]) => void
>[ new Giraffe(), new Elephant()] : (Giraffe | Elephant)[]
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
>bar : (animals: { [n: number]: IAnimal; }) => void
>[ new Giraffe(), new Elephant()] : (Giraffe | Elephant)[]
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()];
>arr : (Giraffe | Elephant)[]
>[new Giraffe(), new Elephant()] : (Giraffe | Elephant)[]
>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
>arr : (Giraffe | Elephant)[]
bar(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]
>bar(arr) : void
>bar : (animals: { [n: number]: IAnimal; }) => void
>arr : (Giraffe | Elephant)[]
|