File: genericCallWithObjectTypeArgsAndConstraints3.errors.txt

package info (click to toggle)
node-typescript 2.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 203,952 kB
  • ctags: 52,987
  • sloc: sh: 11; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,959 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndConstraints3.ts(18,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
  Type argument candidate 'Derived' is not a valid type argument because it is not a supertype of candidate 'Derived2'.
    Property 'y' is missing in type 'Derived2'.


==== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndConstraints3.ts (1 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() }); // error because neither is supertype of the other
             ~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453:   Type argument candidate 'Derived' is not a valid type argument because it is not a supertype of candidate 'Derived2'.
!!! error TS2453:     Property 'y' is missing in type 'Derived2'.
    
    function f2<T extends Base, U extends { x: T; y: T }>(a: U) {
        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 - second argument is processed before x is fixed
    var r4 = f3(x => x, new Base());
    var r5 = f3(x => x, new Derived());
    var r6 = f3(x => x, null);