87 lines
2 KiB
Plaintext
87 lines
2 KiB
Plaintext
=== 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 }) { }
|
|
>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)[]
|
|
|