Skip to content

Commit a5cea93

Browse files
committed
Tests for contextual index signature of union type
Let S be the set of types in U that has a string index signature. If S is not empty, U has a string index signature of a union type of the types of the string index signatures from each type in S. Let S be the set of types in U that has a numeric index signature. If S is not empty, U has a numeric index signature of a union type of the types of the numeric index signatures from each type in S.
1 parent c4e6327 commit a5cea93

3 files changed

Lines changed: 300 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//// [contextualTypeWithUnionTypeIndexSignatures.ts]
2+
//When used as a contextual type, a union type U has those members that are present in any of
3+
// its constituent types, with types that are unions of the respective members in the constituent types.
4+
interface SomeType {
5+
(a: number): number;
6+
}
7+
interface SomeType2 {
8+
(a: number): string;
9+
}
10+
11+
interface IWithNoStringIndexSignature {
12+
foo: string;
13+
}
14+
interface IWithNoNumberIndexSignature {
15+
0: string;
16+
}
17+
interface IWithStringIndexSignature1 {
18+
[a: string]: SomeType;
19+
}
20+
interface IWithStringIndexSignature2 {
21+
[a: string]: SomeType2;
22+
}
23+
interface IWithNumberIndexSignature1 {
24+
[a: number]: SomeType;
25+
}
26+
interface IWithNumberIndexSignature2 {
27+
[a: number]: SomeType2;
28+
}
29+
30+
// When an object literal is contextually typed by a type that includes a string index signature,
31+
// the resulting type of the object literal includes a string index signature with the union type of
32+
// the types of the properties declared in the object literal, or the Undefined type if the object literal
33+
// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index
34+
// signature, the resulting type of the object literal includes a numeric index signature with the union type
35+
// of the types of the numerically named properties(section 3.7.4) declared in the object literal,
36+
// or the Undefined type if the object literal declares no numerically named properties.
37+
38+
// Let S be the set of types in U that has a string index signature.
39+
// If S is not empty, U has a string index signature of a union type of
40+
// the types of the string index signatures from each type in S.
41+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number
42+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
43+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
44+
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
45+
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number
46+
47+
48+
// Let S be the set of types in U that has a numeric index signature.
49+
// If S is not empty, U has a numeric index signature of a union type of
50+
// the types of the numeric index signatures from each type in S.
51+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number
52+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
53+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
54+
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
55+
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number
56+
57+
//// [contextualTypeWithUnionTypeIndexSignatures.js]
58+
// When an object literal is contextually typed by a type that includes a string index signature,
59+
// the resulting type of the object literal includes a string index signature with the union type of
60+
// the types of the properties declared in the object literal, or the Undefined type if the object literal
61+
// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index
62+
// signature, the resulting type of the object literal includes a numeric index signature with the union type
63+
// of the types of the numerically named properties(section 3.7.4) declared in the object literal,
64+
// or the Undefined type if the object literal declares no numerically named properties.
65+
// Let S be the set of types in U that has a string index signature.
66+
// If S is not empty, U has a string index signature of a union type of
67+
// the types of the string index signatures from each type in S.
68+
var x = { z: function (a) { return a; } }; // a should be number
69+
var x = { foo: function (a) { return a; } }; // a should be any
70+
var x = { foo: "hello" };
71+
var x2 = { z: function (a) { return a.toString(); } }; // a should be number
72+
var x2 = { z: function (a) { return a; } }; // a should be number
73+
// Let S be the set of types in U that has a numeric index signature.
74+
// If S is not empty, U has a numeric index signature of a union type of
75+
// the types of the numeric index signatures from each type in S.
76+
var x3 = { 1: function (a) { return a; } }; // a should be number
77+
var x3 = { 0: function (a) { return a; } }; // a should be any
78+
var x3 = { 0: "hello" };
79+
var x4 = { 1: function (a) { return a.toString(); } }; // a should be number
80+
var x4 = { 1: function (a) { return a; } }; // a should be number
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
=== tests/cases/conformance/types/union/contextualTypeWithUnionTypeIndexSignatures.ts ===
2+
//When used as a contextual type, a union type U has those members that are present in any of
3+
// its constituent types, with types that are unions of the respective members in the constituent types.
4+
interface SomeType {
5+
>SomeType : SomeType
6+
7+
(a: number): number;
8+
>a : number
9+
}
10+
interface SomeType2 {
11+
>SomeType2 : SomeType2
12+
13+
(a: number): string;
14+
>a : number
15+
}
16+
17+
interface IWithNoStringIndexSignature {
18+
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
19+
20+
foo: string;
21+
>foo : string
22+
}
23+
interface IWithNoNumberIndexSignature {
24+
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
25+
26+
0: string;
27+
}
28+
interface IWithStringIndexSignature1 {
29+
>IWithStringIndexSignature1 : IWithStringIndexSignature1
30+
31+
[a: string]: SomeType;
32+
>a : string
33+
>SomeType : SomeType
34+
}
35+
interface IWithStringIndexSignature2 {
36+
>IWithStringIndexSignature2 : IWithStringIndexSignature2
37+
38+
[a: string]: SomeType2;
39+
>a : string
40+
>SomeType2 : SomeType2
41+
}
42+
interface IWithNumberIndexSignature1 {
43+
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
44+
45+
[a: number]: SomeType;
46+
>a : number
47+
>SomeType : SomeType
48+
}
49+
interface IWithNumberIndexSignature2 {
50+
>IWithNumberIndexSignature2 : IWithNumberIndexSignature2
51+
52+
[a: number]: SomeType2;
53+
>a : number
54+
>SomeType2 : SomeType2
55+
}
56+
57+
// When an object literal is contextually typed by a type that includes a string index signature,
58+
// the resulting type of the object literal includes a string index signature with the union type of
59+
// the types of the properties declared in the object literal, or the Undefined type if the object literal
60+
// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index
61+
// signature, the resulting type of the object literal includes a numeric index signature with the union type
62+
// of the types of the numerically named properties(section 3.7.4) declared in the object literal,
63+
// or the Undefined type if the object literal declares no numerically named properties.
64+
65+
// Let S be the set of types in U that has a string index signature.
66+
// If S is not empty, U has a string index signature of a union type of
67+
// the types of the string index signatures from each type in S.
68+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number
69+
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
70+
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
71+
>IWithStringIndexSignature1 : IWithStringIndexSignature1
72+
>{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; }
73+
>z : (a: number) => number
74+
>a => a : (a: number) => number
75+
>a : number
76+
>a : number
77+
78+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
79+
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
80+
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
81+
>IWithStringIndexSignature1 : IWithStringIndexSignature1
82+
>{ foo: a => a } : { [x: string]: (a: any) => any; foo: (a: any) => any; }
83+
>foo : (a: any) => any
84+
>a => a : (a: any) => any
85+
>a : any
86+
>a : any
87+
88+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
89+
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
90+
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
91+
>IWithStringIndexSignature1 : IWithStringIndexSignature1
92+
>{ foo: "hello" } : { [x: string]: string; foo: string; }
93+
>foo : string
94+
95+
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
96+
>x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2
97+
>IWithStringIndexSignature1 : IWithStringIndexSignature1
98+
>IWithStringIndexSignature2 : IWithStringIndexSignature2
99+
>{ z: a => a.toString() } : { [x: string]: (a: number) => string; z: (a: number) => string; }
100+
>z : (a: number) => string
101+
>a => a.toString() : (a: number) => string
102+
>a : number
103+
>a.toString() : string
104+
>a.toString : (radix?: number) => string
105+
>a : number
106+
>toString : (radix?: number) => string
107+
108+
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number
109+
>x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2
110+
>IWithStringIndexSignature1 : IWithStringIndexSignature1
111+
>IWithStringIndexSignature2 : IWithStringIndexSignature2
112+
>{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; }
113+
>z : (a: number) => number
114+
>a => a : (a: number) => number
115+
>a : number
116+
>a : number
117+
118+
119+
// Let S be the set of types in U that has a numeric index signature.
120+
// If S is not empty, U has a numeric index signature of a union type of
121+
// the types of the numeric index signatures from each type in S.
122+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number
123+
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
124+
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
125+
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
126+
>{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; }
127+
>a => a : (a: number) => number
128+
>a : number
129+
>a : number
130+
131+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
132+
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
133+
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
134+
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
135+
>{ 0: a => a } : { [x: number]: (a: any) => any; 0: (a: any) => any; }
136+
>a => a : (a: any) => any
137+
>a : any
138+
>a : any
139+
140+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
141+
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
142+
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
143+
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
144+
>{ 0: "hello" } : { [x: number]: string; 0: string; }
145+
146+
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
147+
>x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2
148+
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
149+
>IWithNumberIndexSignature2 : IWithNumberIndexSignature2
150+
>{ 1: a => a.toString() } : { [x: number]: (a: number) => string; 1: (a: number) => string; }
151+
>a => a.toString() : (a: number) => string
152+
>a : number
153+
>a.toString() : string
154+
>a.toString : (radix?: number) => string
155+
>a : number
156+
>toString : (radix?: number) => string
157+
158+
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number
159+
>x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2
160+
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
161+
>IWithNumberIndexSignature2 : IWithNumberIndexSignature2
162+
>{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; }
163+
>a => a : (a: number) => number
164+
>a : number
165+
>a : number
166+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//When used as a contextual type, a union type U has those members that are present in any of
2+
// its constituent types, with types that are unions of the respective members in the constituent types.
3+
interface SomeType {
4+
(a: number): number;
5+
}
6+
interface SomeType2 {
7+
(a: number): string;
8+
}
9+
10+
interface IWithNoStringIndexSignature {
11+
foo: string;
12+
}
13+
interface IWithNoNumberIndexSignature {
14+
0: string;
15+
}
16+
interface IWithStringIndexSignature1 {
17+
[a: string]: SomeType;
18+
}
19+
interface IWithStringIndexSignature2 {
20+
[a: string]: SomeType2;
21+
}
22+
interface IWithNumberIndexSignature1 {
23+
[a: number]: SomeType;
24+
}
25+
interface IWithNumberIndexSignature2 {
26+
[a: number]: SomeType2;
27+
}
28+
29+
// When an object literal is contextually typed by a type that includes a string index signature,
30+
// the resulting type of the object literal includes a string index signature with the union type of
31+
// the types of the properties declared in the object literal, or the Undefined type if the object literal
32+
// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index
33+
// signature, the resulting type of the object literal includes a numeric index signature with the union type
34+
// of the types of the numerically named properties(section 3.7.4) declared in the object literal,
35+
// or the Undefined type if the object literal declares no numerically named properties.
36+
37+
// Let S be the set of types in U that has a string index signature.
38+
// If S is not empty, U has a string index signature of a union type of
39+
// the types of the string index signatures from each type in S.
40+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number
41+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
42+
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
43+
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
44+
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number
45+
46+
47+
// Let S be the set of types in U that has a numeric index signature.
48+
// If S is not empty, U has a numeric index signature of a union type of
49+
// the types of the numeric index signatures from each type in S.
50+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number
51+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
52+
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
53+
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
54+
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number

0 commit comments

Comments
 (0)