File: contextualSignatureInstantiation.types

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (124 lines) | stat: -rw-r--r-- 3,728 bytes parent folder | download | duplicates (4)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
=== tests/cases/conformance/types/typeRelationships/typeInference/contextualSignatureInstantiation.ts ===
// TypeScript Spec, section 4.12.2:
// If e is an expression of a function type that contains exactly one generic call signature and no other members,
// and T is a function type with exactly one non - generic call signature and no other members, then any inferences
// made for type parameters referenced by the parameters of T's call signature are fixed, and e's type is changed
// to a function type with e's call signature instantiated in the context of T's call signature (section 3.8.5).

declare function foo<T>(cb: (x: number, y: string) => T): T;
>foo : <T>(cb: (x: number, y: string) => T) => T
>cb : (x: number, y: string) => T
>x : number
>y : string

declare function bar<T, U, V>(x: T, y: U, cb: (x: T, y: U) => V): V;
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
>x : T
>y : U
>cb : (x: T, y: U) => V
>x : T
>y : U

declare function baz<T, U>(x: T, y: T, cb: (x: T, y: T) => U): U;
>baz : <T, U>(x: T, y: T, cb: (x: T, y: T) => U) => U
>x : T
>y : T
>cb : (x: T, y: T) => U
>x : T
>y : T

declare function g<T>(x: T, y: T): T;
>g : <T>(x: T, y: T) => T
>x : T
>y : T

declare function h<T, U>(x: T, y: U): T[] | U[];
>h : <T, U>(x: T, y: U) => T[] | U[]
>x : T
>y : U

var a: number;
>a : number

var a = bar(1, 1, g);      // Should be number
>a : number
>bar(1, 1, g) : number
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
>1 : 1
>1 : 1
>g : <T>(x: T, y: T) => T

var a = baz(1, 1, g);      // Should be number
>a : number
>baz(1, 1, g) : number
>baz : <T, U>(x: T, y: T, cb: (x: T, y: T) => U) => U
>1 : 1
>1 : 1
>g : <T>(x: T, y: T) => T

var b: number | string;
>b : string | number

var b = foo(g);            // Error, number and string are disjoint types
>b : string | number
>foo(g) : unknown
>foo : <T>(cb: (x: number, y: string) => T) => T
>g : <T>(x: T, y: T) => T

var b = bar(1, "one", g);  // Error, number and string are disjoint types
>b : string | number
>bar(1, "one", g) : unknown
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
>1 : 1
>"one" : "one"
>g : <T>(x: T, y: T) => T

var b = bar("one", 1, g);  // Error, number and string are disjoint types
>b : string | number
>bar("one", 1, g) : unknown
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
>"one" : "one"
>1 : 1
>g : <T>(x: T, y: T) => T

var b = baz(b, b, g);      // Should be number | string
>b : string | number
>baz(b, b, g) : string | number
>baz : <T, U>(x: T, y: T, cb: (x: T, y: T) => U) => U
>b : string | number
>b : string | number
>g : <T>(x: T, y: T) => T

var d: number[] | string[];
>d : number[] | string[]

var d = foo(h);            // Should be number[] | string[]
>d : number[] | string[]
>foo(h) : number[] | string[]
>foo : <T>(cb: (x: number, y: string) => T) => T
>h : <T, U>(x: T, y: U) => T[] | U[]

var d = bar(1, "one", h);  // Should be number[] | string[]
>d : number[] | string[]
>bar(1, "one", h) : number[] | string[]
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
>1 : 1
>"one" : "one"
>h : <T, U>(x: T, y: U) => T[] | U[]

var d = bar("one", 1, h);  // Should be number[] | string[]
>d : number[] | string[]
>bar("one", 1, h) : number[] | string[]
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
>"one" : "one"
>1 : 1
>h : <T, U>(x: T, y: U) => T[] | U[]

var d = baz(d, d, g);      // Should be number[] | string[]
>d : number[] | string[]
>baz(d, d, g) : number[] | string[]
>baz : <T, U>(x: T, y: T, cb: (x: T, y: T) => U) => U
>d : number[] | string[]
>d : number[] | string[]
>g : <T>(x: T, y: T) => T