TypeScript/tests/baselines/reference/arrayLiteralContextualType.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-07-13 01:04:16 +02:00
//// [arrayLiteralContextualType.ts]
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()];
2014-10-10 23:41:14 +02:00
foo(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]
bar(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]
2014-07-13 01:04:16 +02:00
//// [arrayLiteralContextualType.js]
var Giraffe = (function () {
function Giraffe() {
this.name = "Giraffe";
this.neckLength = "3m";
}
return Giraffe;
})();
var Elephant = (function () {
function Elephant() {
this.name = "Elephant";
this.trunkDiameter = "20cm";
}
return Elephant;
})();
function foo(animals) { }
function bar(animals) { }
2014-07-13 01:04:16 +02:00
foo([
new Giraffe(),
new Elephant()
]); // Legal because of the contextual type IAnimal provided by the parameter
2014-07-13 01:04:16 +02:00
bar([
new Giraffe(),
new Elephant()
]); // Legal because of the contextual type IAnimal provided by the parameter
2014-07-13 01:04:16 +02:00
var arr = [new Giraffe(), new Elephant()];
2014-10-10 23:41:14 +02:00
foo(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]
bar(arr); // ok because arr is Array<Giraffe|Elephant> not {}[]