2014-08-15 23:33:16 +02:00
|
|
|
=== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndConstraints2.ts ===
|
|
|
|
// Generic call with constraints infering type parameter from object member properties
|
|
|
|
// No errors expected
|
|
|
|
|
|
|
|
class Base {
|
|
|
|
>Base : Base
|
|
|
|
|
|
|
|
x: string;
|
|
|
|
>x : string
|
|
|
|
}
|
|
|
|
class Derived extends Base {
|
|
|
|
>Derived : Derived
|
|
|
|
>Base : Base
|
|
|
|
|
|
|
|
y: string;
|
|
|
|
>y : string
|
|
|
|
}
|
|
|
|
|
|
|
|
function f<T extends Base>(x: { foo: T; bar: T }) {
|
|
|
|
>f : <T extends Base>(x: { foo: T; bar: T; }) => T
|
|
|
|
>T : T
|
|
|
|
>Base : Base
|
|
|
|
>x : { foo: T; bar: T; }
|
|
|
|
>foo : T
|
|
|
|
>T : T
|
|
|
|
>bar : T
|
|
|
|
>T : T
|
|
|
|
|
|
|
|
var r: T;
|
|
|
|
>r : T
|
|
|
|
>T : T
|
|
|
|
|
|
|
|
return r;
|
|
|
|
>r : T
|
|
|
|
}
|
|
|
|
var r = f({ foo: new Base(), bar: new Derived() });
|
|
|
|
>r : Base
|
|
|
|
>f({ foo: new Base(), bar: new Derived() }) : Base
|
|
|
|
>f : <T extends Base>(x: { foo: T; bar: T; }) => T
|
|
|
|
>{ foo: new Base(), bar: new Derived() } : { foo: Base; bar: Derived; }
|
|
|
|
>foo : Base
|
|
|
|
>new Base() : Base
|
|
|
|
>Base : typeof Base
|
|
|
|
>bar : Derived
|
|
|
|
>new Derived() : Derived
|
|
|
|
>Derived : typeof Derived
|
|
|
|
|
|
|
|
var r2 = f({ foo: new Derived(), bar: new Derived() });
|
|
|
|
>r2 : Derived
|
|
|
|
>f({ foo: new Derived(), bar: new Derived() }) : Derived
|
|
|
|
>f : <T extends Base>(x: { foo: T; bar: T; }) => T
|
|
|
|
>{ foo: new Derived(), bar: new Derived() } : { foo: Derived; bar: Derived; }
|
|
|
|
>foo : Derived
|
|
|
|
>new Derived() : Derived
|
|
|
|
>Derived : typeof Derived
|
|
|
|
>bar : Derived
|
|
|
|
>new Derived() : Derived
|
|
|
|
>Derived : typeof Derived
|
|
|
|
|
|
|
|
|
|
|
|
interface I<T> {
|
|
|
|
>I : I<T>
|
|
|
|
>T : T
|
|
|
|
|
|
|
|
a: T;
|
|
|
|
>a : T
|
|
|
|
>T : T
|
|
|
|
}
|
|
|
|
function f2<T extends Base>(x: I<T>) {
|
|
|
|
>f2 : <T extends Base>(x: I<T>) => T
|
|
|
|
>T : T
|
|
|
|
>Base : Base
|
|
|
|
>x : I<T>
|
2014-08-19 04:56:03 +02:00
|
|
|
>I : I<T>
|
2014-08-15 23:33:16 +02:00
|
|
|
>T : T
|
|
|
|
|
|
|
|
var r: T;
|
|
|
|
>r : T
|
|
|
|
>T : T
|
|
|
|
|
|
|
|
return r;
|
|
|
|
>r : T
|
|
|
|
}
|
|
|
|
var i: I<Derived>;
|
|
|
|
>i : I<Derived>
|
2014-08-19 04:56:03 +02:00
|
|
|
>I : I<T>
|
2014-08-15 23:33:16 +02:00
|
|
|
>Derived : Derived
|
|
|
|
|
|
|
|
var r3 = f2(i);
|
|
|
|
>r3 : Derived
|
|
|
|
>f2(i) : Derived
|
|
|
|
>f2 : <T extends Base>(x: I<T>) => T
|
|
|
|
>i : I<Derived>
|
|
|
|
|
|
|
|
|
|
|
|
function f3<T extends Base>(x: T, y: (a: T) => T) {
|
|
|
|
>f3 : <T extends Base>(x: T, y: (a: T) => T) => T
|
|
|
|
>T : T
|
|
|
|
>Base : Base
|
|
|
|
>x : T
|
|
|
|
>T : T
|
|
|
|
>y : (a: T) => T
|
|
|
|
>a : T
|
|
|
|
>T : T
|
|
|
|
>T : T
|
|
|
|
|
|
|
|
return y(null);
|
|
|
|
>y(null) : T
|
|
|
|
>y : (a: T) => T
|
|
|
|
}
|
|
|
|
var r4 = f3(new Base(), x => x);
|
|
|
|
>r4 : Base
|
|
|
|
>f3(new Base(), x => x) : Base
|
|
|
|
>f3 : <T extends Base>(x: T, y: (a: T) => T) => T
|
|
|
|
>new Base() : Base
|
|
|
|
>Base : typeof Base
|
|
|
|
>x => x : (x: Base) => Base
|
|
|
|
>x : Base
|
|
|
|
>x : Base
|
|
|
|
|
|
|
|
var r5 = f3(new Derived(), x => x);
|
|
|
|
>r5 : Derived
|
|
|
|
>f3(new Derived(), x => x) : Derived
|
|
|
|
>f3 : <T extends Base>(x: T, y: (a: T) => T) => T
|
|
|
|
>new Derived() : Derived
|
|
|
|
>Derived : typeof Derived
|
|
|
|
>x => x : (x: Derived) => Derived
|
|
|
|
>x : Derived
|
|
|
|
>x : Derived
|
|
|
|
|
|
|
|
var r6 = f3(null, null); // any
|
|
|
|
>r6 : any
|
|
|
|
>f3(null, null) : any
|
|
|
|
>f3 : <T extends Base>(x: T, y: (a: T) => T) => T
|
|
|
|
|
|
|
|
var r7 = f3(null, x => x); // any
|
|
|
|
>r7 : any
|
|
|
|
>f3(null, x => x) : any
|
|
|
|
>f3 : <T extends Base>(x: T, y: (a: T) => T) => T
|
|
|
|
>x => x : (x: any) => any
|
|
|
|
>x : any
|
|
|
|
>x : any
|
|
|
|
|