46 lines
No EOL
1.9 KiB
Text
46 lines
No EOL
1.9 KiB
Text
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndConstraints3.ts(18,10): error TS2448: The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly.
|
|
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndConstraints3.ts(20,29): error TS2313: Constraint of a type parameter cannot reference any type parameter from the same type parameter list.
|
|
|
|
|
|
==== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndConstraints3.ts (2 errors) ====
|
|
// Generic call with constraints infering type parameter from object member properties
|
|
|
|
class Base {
|
|
x: string;
|
|
}
|
|
class Derived extends Base {
|
|
y: string;
|
|
}
|
|
class Derived2 extends Base {
|
|
z: string;
|
|
}
|
|
|
|
function f<T extends Base>(a: { x: T; y: T }) {
|
|
var r: T;
|
|
return r;
|
|
}
|
|
|
|
var r1 = f({ x: new Derived(), y: new Derived2() }); // ok, both extend Base
|
|
~
|
|
!!! error TS2448: The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly.
|
|
|
|
function f2<T extends Base, U extends { x: T; y: T }>(a: U) {
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
!!! error TS2313: Constraint of a type parameter cannot reference any type parameter from the same type parameter list.
|
|
var r: T;
|
|
return r;
|
|
}
|
|
|
|
var r2 = f2({ x: new Derived(), y: new Derived2() }); // ok
|
|
var r3 = f2({ x: new Derived(), y: new Derived2() }); // ok
|
|
|
|
|
|
function f3<T extends Base>(y: (a: T) => T, x: T) {
|
|
return y(null);
|
|
}
|
|
|
|
// all ok - T gets fixed too early, but then defaults to Base and everything works out
|
|
var r4 = f3(x => x, new Base());
|
|
var r5 = f3(x => x, new Derived());
|
|
var r6 = f3(x => x, null);
|
|
|