Skip to content

Commit 3da1315

Browse files
committed
Merge pull request #2761 from Microsoft/conformanceContextuallyTypedFuncExp
Conformance test for update spec section 4.9.3 contextual typing in function expression
2 parents 75c0bbc + 94ed1d8 commit 3da1315

7 files changed

+607
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//// [functionExpressionContextualTyping1.ts]
2+
// When a function expression with no type parameters and no parameter type annotations
3+
// is contextually typed (section 4.19) by a type T and a contextual signature S can be extracted from T
4+
5+
enum E { red, blue }
6+
7+
// A contextual signature S is extracted from a function type T as follows:
8+
// If T is a function type with exactly one call signature, and if that call signature is non- generic, S is that signature.
9+
10+
var a0: (n: number, s: string) => number = (num, str) => {
11+
num.toExponential();
12+
return 0;
13+
}
14+
15+
class Class<T> {
16+
foo() { }
17+
}
18+
19+
var a1: (c: Class<Number>) => number = (a1) => {
20+
a1.foo();
21+
return 1;
22+
}
23+
24+
// A contextual signature S is extracted from a function type T as follows:
25+
// If T is a union type, let U be the set of element types in T that have call signatures.
26+
// If each type in U has exactly one call signature and that call signature is non- generic,
27+
// and if all of the signatures are identical ignoring return types,
28+
// then S is a signature with the same parameters and a union of the return types.
29+
var b1: ((s: string, w: boolean) => void) | ((s: string, w: boolean) => string);
30+
b1 = (k, h) => { };
31+
var b2: typeof a0 | ((n: number, s: string) => string);
32+
b2 = (foo, bar) => { return foo + 1; }
33+
b2 = (foo, bar) => { return "hello"; }
34+
var b3: (name: string, num: number, boo: boolean) => void;
35+
b3 = (name, number) => { };
36+
37+
var b4: (n: E) => string = (number = 1) => { return "hello"; };
38+
var b5: (n: {}) => string = (number = "string") => { return "hello"; };
39+
40+
// A contextual signature S is extracted from a function type T as follows:
41+
// Otherwise, no contextual signature can be extracted from T and S is undefined.
42+
var b6: ((s: string, w: boolean) => void) | ((n: number) => number);
43+
var b7: ((s: string, w: boolean) => void) | ((s: string, w: number) => string);
44+
b6 = (k) => { k.toLowerCase() };
45+
b6 = (i) => {
46+
i.toExponential();
47+
return i;
48+
}; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
49+
b7 = (j, m) => { }; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
50+
51+
class C<T, U> {
52+
constructor() {
53+
var k: ((j: T, k: U) => (T|U)[]) | ((j: number,k :U) => number[]) = (j, k) => {
54+
return [j, k];
55+
} // Per spec, no contextual signature can be extracted in this case.
56+
}
57+
}
58+
59+
//// [functionExpressionContextualTyping1.js]
60+
// When a function expression with no type parameters and no parameter type annotations
61+
// is contextually typed (section 4.19) by a type T and a contextual signature S can be extracted from T
62+
var E;
63+
(function (E) {
64+
E[E["red"] = 0] = "red";
65+
E[E["blue"] = 1] = "blue";
66+
})(E || (E = {}));
67+
// A contextual signature S is extracted from a function type T as follows:
68+
// If T is a function type with exactly one call signature, and if that call signature is non- generic, S is that signature.
69+
var a0 = function (num, str) {
70+
num.toExponential();
71+
return 0;
72+
};
73+
var Class = (function () {
74+
function Class() {
75+
}
76+
Class.prototype.foo = function () { };
77+
return Class;
78+
})();
79+
var a1 = function (a1) {
80+
a1.foo();
81+
return 1;
82+
};
83+
// A contextual signature S is extracted from a function type T as follows:
84+
// If T is a union type, let U be the set of element types in T that have call signatures.
85+
// If each type in U has exactly one call signature and that call signature is non- generic,
86+
// and if all of the signatures are identical ignoring return types,
87+
// then S is a signature with the same parameters and a union of the return types.
88+
var b1;
89+
b1 = function (k, h) { };
90+
var b2;
91+
b2 = function (foo, bar) { return foo + 1; };
92+
b2 = function (foo, bar) { return "hello"; };
93+
var b3;
94+
b3 = function (name, number) { };
95+
var b4 = function (number) {
96+
if (number === void 0) { number = 1; }
97+
return "hello";
98+
};
99+
var b5 = function (number) {
100+
if (number === void 0) { number = "string"; }
101+
return "hello";
102+
};
103+
// A contextual signature S is extracted from a function type T as follows:
104+
// Otherwise, no contextual signature can be extracted from T and S is undefined.
105+
var b6;
106+
var b7;
107+
b6 = function (k) { k.toLowerCase(); };
108+
b6 = function (i) {
109+
i.toExponential();
110+
return i;
111+
}; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
112+
b7 = function (j, m) { }; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
113+
var C = (function () {
114+
function C() {
115+
var k = function (j, k) {
116+
return [j, k];
117+
}; // Per spec, no contextual signature can be extracted in this case.
118+
}
119+
return C;
120+
})();
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
=== tests/cases/conformance/expressions/contextualTyping/functionExpressionContextualTyping1.ts ===
2+
// When a function expression with no type parameters and no parameter type annotations
3+
// is contextually typed (section 4.19) by a type T and a contextual signature S can be extracted from T
4+
5+
enum E { red, blue }
6+
>E : Symbol(E, Decl(functionExpressionContextualTyping1.ts, 0, 0))
7+
>red : Symbol(E.red, Decl(functionExpressionContextualTyping1.ts, 3, 8))
8+
>blue : Symbol(E.blue, Decl(functionExpressionContextualTyping1.ts, 3, 13))
9+
10+
// A contextual signature S is extracted from a function type T as follows:
11+
// If T is a function type with exactly one call signature, and if that call signature is non- generic, S is that signature.
12+
13+
var a0: (n: number, s: string) => number = (num, str) => {
14+
>a0 : Symbol(a0, Decl(functionExpressionContextualTyping1.ts, 8, 3))
15+
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 8, 9))
16+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 8, 19))
17+
>num : Symbol(num, Decl(functionExpressionContextualTyping1.ts, 8, 44))
18+
>str : Symbol(str, Decl(functionExpressionContextualTyping1.ts, 8, 48))
19+
20+
num.toExponential();
21+
>num.toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, 469, 45))
22+
>num : Symbol(num, Decl(functionExpressionContextualTyping1.ts, 8, 44))
23+
>toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, 469, 45))
24+
25+
return 0;
26+
}
27+
28+
class Class<T> {
29+
>Class : Symbol(Class, Decl(functionExpressionContextualTyping1.ts, 11, 1))
30+
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 13, 12))
31+
32+
foo() { }
33+
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 13, 16))
34+
}
35+
36+
var a1: (c: Class<Number>) => number = (a1) => {
37+
>a1 : Symbol(a1, Decl(functionExpressionContextualTyping1.ts, 17, 3))
38+
>c : Symbol(c, Decl(functionExpressionContextualTyping1.ts, 17, 9))
39+
>Class : Symbol(Class, Decl(functionExpressionContextualTyping1.ts, 11, 1))
40+
>Number : Symbol(Number, Decl(lib.d.ts, 456, 40), Decl(lib.d.ts, 518, 11))
41+
>a1 : Symbol(a1, Decl(functionExpressionContextualTyping1.ts, 17, 40))
42+
43+
a1.foo();
44+
>a1.foo : Symbol(Class.foo, Decl(functionExpressionContextualTyping1.ts, 13, 16))
45+
>a1 : Symbol(a1, Decl(functionExpressionContextualTyping1.ts, 17, 40))
46+
>foo : Symbol(Class.foo, Decl(functionExpressionContextualTyping1.ts, 13, 16))
47+
48+
return 1;
49+
}
50+
51+
// A contextual signature S is extracted from a function type T as follows:
52+
// If T is a union type, let U be the set of element types in T that have call signatures.
53+
// If each type in U has exactly one call signature and that call signature is non- generic,
54+
// and if all of the signatures are identical ignoring return types,
55+
// then S is a signature with the same parameters and a union of the return types.
56+
var b1: ((s: string, w: boolean) => void) | ((s: string, w: boolean) => string);
57+
>b1 : Symbol(b1, Decl(functionExpressionContextualTyping1.ts, 27, 3))
58+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 27, 10))
59+
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 27, 20))
60+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 27, 46))
61+
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 27, 56))
62+
63+
b1 = (k, h) => { };
64+
>b1 : Symbol(b1, Decl(functionExpressionContextualTyping1.ts, 27, 3))
65+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 28, 6))
66+
>h : Symbol(h, Decl(functionExpressionContextualTyping1.ts, 28, 8))
67+
68+
var b2: typeof a0 | ((n: number, s: string) => string);
69+
>b2 : Symbol(b2, Decl(functionExpressionContextualTyping1.ts, 29, 3))
70+
>a0 : Symbol(a0, Decl(functionExpressionContextualTyping1.ts, 8, 3))
71+
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 29, 22))
72+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 29, 32))
73+
74+
b2 = (foo, bar) => { return foo + 1; }
75+
>b2 : Symbol(b2, Decl(functionExpressionContextualTyping1.ts, 29, 3))
76+
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 30, 6))
77+
>bar : Symbol(bar, Decl(functionExpressionContextualTyping1.ts, 30, 10))
78+
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 30, 6))
79+
80+
b2 = (foo, bar) => { return "hello"; }
81+
>b2 : Symbol(b2, Decl(functionExpressionContextualTyping1.ts, 29, 3))
82+
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 31, 6))
83+
>bar : Symbol(bar, Decl(functionExpressionContextualTyping1.ts, 31, 10))
84+
85+
var b3: (name: string, num: number, boo: boolean) => void;
86+
>b3 : Symbol(b3, Decl(functionExpressionContextualTyping1.ts, 32, 3))
87+
>name : Symbol(name, Decl(functionExpressionContextualTyping1.ts, 32, 9))
88+
>num : Symbol(num, Decl(functionExpressionContextualTyping1.ts, 32, 22))
89+
>boo : Symbol(boo, Decl(functionExpressionContextualTyping1.ts, 32, 35))
90+
91+
b3 = (name, number) => { };
92+
>b3 : Symbol(b3, Decl(functionExpressionContextualTyping1.ts, 32, 3))
93+
>name : Symbol(name, Decl(functionExpressionContextualTyping1.ts, 33, 6))
94+
>number : Symbol(number, Decl(functionExpressionContextualTyping1.ts, 33, 11))
95+
96+
var b4: (n: E) => string = (number = 1) => { return "hello"; };
97+
>b4 : Symbol(b4, Decl(functionExpressionContextualTyping1.ts, 35, 3))
98+
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 35, 9))
99+
>E : Symbol(E, Decl(functionExpressionContextualTyping1.ts, 0, 0))
100+
>number : Symbol(number, Decl(functionExpressionContextualTyping1.ts, 35, 28))
101+
102+
var b5: (n: {}) => string = (number = "string") => { return "hello"; };
103+
>b5 : Symbol(b5, Decl(functionExpressionContextualTyping1.ts, 36, 3))
104+
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 36, 9))
105+
>number : Symbol(number, Decl(functionExpressionContextualTyping1.ts, 36, 29))
106+
107+
// A contextual signature S is extracted from a function type T as follows:
108+
// Otherwise, no contextual signature can be extracted from T and S is undefined.
109+
var b6: ((s: string, w: boolean) => void) | ((n: number) => number);
110+
>b6 : Symbol(b6, Decl(functionExpressionContextualTyping1.ts, 40, 3))
111+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 40, 10))
112+
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 40, 20))
113+
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 40, 46))
114+
115+
var b7: ((s: string, w: boolean) => void) | ((s: string, w: number) => string);
116+
>b7 : Symbol(b7, Decl(functionExpressionContextualTyping1.ts, 41, 3))
117+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 41, 10))
118+
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 41, 20))
119+
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 41, 46))
120+
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 41, 56))
121+
122+
b6 = (k) => { k.toLowerCase() };
123+
>b6 : Symbol(b6, Decl(functionExpressionContextualTyping1.ts, 40, 3))
124+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 42, 6))
125+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 42, 6))
126+
127+
b6 = (i) => {
128+
>b6 : Symbol(b6, Decl(functionExpressionContextualTyping1.ts, 40, 3))
129+
>i : Symbol(i, Decl(functionExpressionContextualTyping1.ts, 43, 6))
130+
131+
i.toExponential();
132+
>i : Symbol(i, Decl(functionExpressionContextualTyping1.ts, 43, 6))
133+
134+
return i;
135+
>i : Symbol(i, Decl(functionExpressionContextualTyping1.ts, 43, 6))
136+
137+
}; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
138+
b7 = (j, m) => { }; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
139+
>b7 : Symbol(b7, Decl(functionExpressionContextualTyping1.ts, 41, 3))
140+
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 47, 6))
141+
>m : Symbol(m, Decl(functionExpressionContextualTyping1.ts, 47, 8))
142+
143+
class C<T, U> {
144+
>C : Symbol(C, Decl(functionExpressionContextualTyping1.ts, 47, 19))
145+
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 49, 8))
146+
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
147+
148+
constructor() {
149+
var k: ((j: T, k: U) => (T|U)[]) | ((j: number,k :U) => number[]) = (j, k) => {
150+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 11))
151+
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 17))
152+
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 49, 8))
153+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 22))
154+
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
155+
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 49, 8))
156+
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
157+
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 45))
158+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 55))
159+
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
160+
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 77))
161+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 79))
162+
163+
return [j, k];
164+
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 77))
165+
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 79))
166+
167+
} // Per spec, no contextual signature can be extracted in this case.
168+
}
169+
}

0 commit comments

Comments
 (0)