TypeScript/tests/baselines/reference/propertyAccessOnTypeParameterWithConstraints2.types
2015-04-15 16:44:20 -07:00

277 lines
5.6 KiB
Plaintext

=== tests/cases/conformance/types/typeParameters/typeParameterLists/propertyAccessOnTypeParameterWithConstraints2.ts ===
// generic types should behave as if they have properties of their constraint type
class A {
>A : A
foo(): string { return ''; }
>foo : () => string
>'' : string
}
class B extends A {
>B : B
>A : A
bar(): string {
>bar : () => string
return '';
>'' : string
}
}
class C<U extends A, T extends A> {
>C : C<U, T>
>U : U
>A : A
>T : T
>A : A
f() {
>f : () => string
var x: U;
>x : U
>U : U
var a = x['foo'](); // should be string
>a : string
>x['foo']() : string
>x['foo'] : () => string
>x : U
>'foo' : string
return a + x.foo();
>a + x.foo() : string
>a : string
>x.foo() : string
>x.foo : () => string
>x : U
>foo : () => string
}
g(x: U) {
>g : (x: U) => string
>x : U
>U : U
var a = x['foo'](); // should be string
>a : string
>x['foo']() : string
>x['foo'] : () => string
>x : U
>'foo' : string
return a + x.foo();
>a + x.foo() : string
>a : string
>x.foo() : string
>x.foo : () => string
>x : U
>foo : () => string
}
}
//class C<U extends T, T extends A> {
// f() {
// var x: U;
// var a = x['foo'](); // should be string
// return a + x.foo();
// }
// g(x: U) {
// var a = x['foo'](); // should be string
// return a + x.foo();
// }
//}
var r1 = (new C<B, A>()).f();
>r1 : string
>(new C<B, A>()).f() : string
>(new C<B, A>()).f : () => string
>(new C<B, A>()) : C<B, A>
>new C<B, A>() : C<B, A>
>C : typeof C
>B : B
>A : A
>f : () => string
var r1b = (new C<B, A>()).g(new B());
>r1b : string
>(new C<B, A>()).g(new B()) : string
>(new C<B, A>()).g : (x: B) => string
>(new C<B, A>()) : C<B, A>
>new C<B, A>() : C<B, A>
>C : typeof C
>B : B
>A : A
>g : (x: B) => string
>new B() : B
>B : typeof B
interface I<U extends A, T extends A> {
>I : I<U, T>
>U : U
>A : A
>T : T
>A : A
foo: U;
>foo : U
>U : U
}
//interface I<U extends T, T extends A> {
// foo: U;
//}
var i: I<B, A>;
>i : I<B, A>
>I : I<U, T>
>B : B
>A : A
var r2 = i.foo.foo();
>r2 : string
>i.foo.foo() : string
>i.foo.foo : () => string
>i.foo : B
>i : I<B, A>
>foo : B
>foo : () => string
var r2b = i.foo['foo']();
>r2b : string
>i.foo['foo']() : string
>i.foo['foo'] : () => string
>i.foo : B
>i : I<B, A>
>foo : B
>'foo' : string
var a: {
>a : { <U extends A, T extends A>(): U; <U extends A, T extends A>(x: U): U; <U extends A, T extends A>(x: U, y: T): U; }
<U extends A, T extends A>(): U;
>U : U
>A : A
>T : T
>A : A
>U : U
<U extends A, T extends A>(x: U): U;
>U : U
>A : A
>T : T
>A : A
>x : U
>U : U
>U : U
<U extends A, T extends A>(x: U, y: T): U;
>U : U
>A : A
>T : T
>A : A
>x : U
>U : U
>y : T
>T : T
>U : U
}
//var a: {
// <U extends T, T extends A>(): U;
// <U extends T, T extends A>(x: U): U;
// <U extends T, T extends A>(x: U, y: T): U;
//}
var r3 = a<A, A>().foo();
>r3 : string
>a<A, A>().foo() : string
>a<A, A>().foo : () => string
>a<A, A>() : A
>a : { <U extends A, T extends A>(): U; <U extends A, T extends A>(x: U): U; <U extends A, T extends A>(x: U, y: T): U; }
>A : A
>A : A
>foo : () => string
var r3b = a()['foo']();
>r3b : string
>a()['foo']() : string
>a()['foo'] : () => string
>a() : A
>a : { <U extends A, T extends A>(): U; <U extends A, T extends A>(x: U): U; <U extends A, T extends A>(x: U, y: T): U; }
>'foo' : string
// parameter supplied for type argument inference to succeed
var aB = new B();
>aB : B
>new B() : B
>B : typeof B
var r3c = a(aB, aB).foo();
>r3c : string
>a(aB, aB).foo() : string
>a(aB, aB).foo : () => string
>a(aB, aB) : B
>a : { <U extends A, T extends A>(): U; <U extends A, T extends A>(x: U): U; <U extends A, T extends A>(x: U, y: T): U; }
>aB : B
>aB : B
>foo : () => string
var r3d = a(aB, aB)['foo']();
>r3d : string
>a(aB, aB)['foo']() : string
>a(aB, aB)['foo'] : () => string
>a(aB, aB) : B
>a : { <U extends A, T extends A>(): U; <U extends A, T extends A>(x: U): U; <U extends A, T extends A>(x: U, y: T): U; }
>aB : B
>aB : B
>'foo' : string
var b = {
>b : { foo: <U extends A, T extends A>(x: U, y: T) => string; }
>{ foo: <U extends A, T extends A>(x: U, y: T) => { var a = x['foo'](); // should be string return a + x.foo(); }} : { foo: <U extends A, T extends A>(x: U, y: T) => string; }
foo: <U extends A, T extends A>(x: U, y: T) => {
>foo : <U extends A, T extends A>(x: U, y: T) => string
><U extends A, T extends A>(x: U, y: T) => { var a = x['foo'](); // should be string return a + x.foo(); } : <U extends A, T extends A>(x: U, y: T) => string
>U : U
>A : A
>T : T
>A : A
>x : U
>U : U
>y : T
>T : T
var a = x['foo'](); // should be string
>a : string
>x['foo']() : string
>x['foo'] : () => string
>x : U
>'foo' : string
return a + x.foo();
>a + x.foo() : string
>a : string
>x.foo() : string
>x.foo : () => string
>x : U
>foo : () => string
}
}
//var b = {
// foo: <U extends T, T extends A>(x: U, y: T) => {
// var a = x['foo'](); // should be string
// return a + x.foo();
// }
//}
var r4 = b.foo(aB, aB); // no inferences for T so constraint isn't satisfied, error
>r4 : string
>b.foo(aB, aB) : string
>b.foo : <U extends A, T extends A>(x: U, y: T) => string
>b : { foo: <U extends A, T extends A>(x: U, y: T) => string; }
>foo : <U extends A, T extends A>(x: U, y: T) => string
>aB : B
>aB : B