Skip to content

Commit 49b5b5a

Browse files
committed
Tests for union of index signatures:
• If each type in U has a string index signature, U has a string index signature of a union type of the types of the string index signatures from each type in U. • If each type in U has a numeric index signature, U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
1 parent c2b2c30 commit 49b5b5a

3 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [unionTypeIndexSignature.ts]
2+
var numOrDate: number | Date;
3+
var anyVar: number;
4+
5+
// If each type in U has a string index signature,
6+
// U has a string index signature of a union type of the types of the string index signatures from each type in U.
7+
8+
var unionOfDifferentReturnType: { [a: string]: number; } | { [a: string]: Date; };
9+
numOrDate = unionOfDifferentReturnType["hello"]; // number | Date
10+
numOrDate = unionOfDifferentReturnType[10]; // number | Date
11+
12+
var unionOfTypesWithAndWithoutStringSignature: { [a: string]: number; } | boolean;
13+
anyVar = unionOfTypesWithAndWithoutStringSignature["hello"]; // any
14+
anyVar = unionOfTypesWithAndWithoutStringSignature[10]; // any
15+
16+
// If each type in U has a numeric index signature,
17+
// U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
18+
var unionOfDifferentReturnType1: { [a: number]: number; } | { [a: number]: Date; };
19+
numOrDate = unionOfDifferentReturnType1["hello"]; // any
20+
numOrDate = unionOfDifferentReturnType1[10]; // number | Date
21+
22+
var unionOfTypesWithAndWithoutStringSignature1: { [a: number]: number; } | boolean;
23+
anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"]; // any
24+
anyVar = unionOfTypesWithAndWithoutStringSignature1[10]; // any
25+
26+
//// [unionTypeIndexSignature.js]
27+
var numOrDate;
28+
var anyVar;
29+
// If each type in U has a string index signature,
30+
// U has a string index signature of a union type of the types of the string index signatures from each type in U.
31+
var unionOfDifferentReturnType;
32+
numOrDate = unionOfDifferentReturnType["hello"]; // number | Date
33+
numOrDate = unionOfDifferentReturnType[10]; // number | Date
34+
var unionOfTypesWithAndWithoutStringSignature;
35+
anyVar = unionOfTypesWithAndWithoutStringSignature["hello"]; // any
36+
anyVar = unionOfTypesWithAndWithoutStringSignature[10]; // any
37+
// If each type in U has a numeric index signature,
38+
// U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
39+
var unionOfDifferentReturnType1;
40+
numOrDate = unionOfDifferentReturnType1["hello"]; // any
41+
numOrDate = unionOfDifferentReturnType1[10]; // number | Date
42+
var unionOfTypesWithAndWithoutStringSignature1;
43+
anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"]; // any
44+
anyVar = unionOfTypesWithAndWithoutStringSignature1[10]; // any
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
=== tests/cases/conformance/types/union/unionTypeIndexSignature.ts ===
2+
var numOrDate: number | Date;
3+
>numOrDate : number | Date
4+
>Date : Date
5+
6+
var anyVar: number;
7+
>anyVar : number
8+
9+
// If each type in U has a string index signature,
10+
// U has a string index signature of a union type of the types of the string index signatures from each type in U.
11+
12+
var unionOfDifferentReturnType: { [a: string]: number; } | { [a: string]: Date; };
13+
>unionOfDifferentReturnType : { [x: string]: number; } | { [x: string]: Date; }
14+
>a : string
15+
>a : string
16+
>Date : Date
17+
18+
numOrDate = unionOfDifferentReturnType["hello"]; // number | Date
19+
>numOrDate = unionOfDifferentReturnType["hello"] : number | Date
20+
>numOrDate : number | Date
21+
>unionOfDifferentReturnType["hello"] : number | Date
22+
>unionOfDifferentReturnType : { [x: string]: number; } | { [x: string]: Date; }
23+
24+
numOrDate = unionOfDifferentReturnType[10]; // number | Date
25+
>numOrDate = unionOfDifferentReturnType[10] : number | Date
26+
>numOrDate : number | Date
27+
>unionOfDifferentReturnType[10] : number | Date
28+
>unionOfDifferentReturnType : { [x: string]: number; } | { [x: string]: Date; }
29+
30+
var unionOfTypesWithAndWithoutStringSignature: { [a: string]: number; } | boolean;
31+
>unionOfTypesWithAndWithoutStringSignature : boolean | { [x: string]: number; }
32+
>a : string
33+
34+
anyVar = unionOfTypesWithAndWithoutStringSignature["hello"]; // any
35+
>anyVar = unionOfTypesWithAndWithoutStringSignature["hello"] : any
36+
>anyVar : number
37+
>unionOfTypesWithAndWithoutStringSignature["hello"] : any
38+
>unionOfTypesWithAndWithoutStringSignature : boolean | { [x: string]: number; }
39+
40+
anyVar = unionOfTypesWithAndWithoutStringSignature[10]; // any
41+
>anyVar = unionOfTypesWithAndWithoutStringSignature[10] : any
42+
>anyVar : number
43+
>unionOfTypesWithAndWithoutStringSignature[10] : any
44+
>unionOfTypesWithAndWithoutStringSignature : boolean | { [x: string]: number; }
45+
46+
// If each type in U has a numeric index signature,
47+
// U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
48+
var unionOfDifferentReturnType1: { [a: number]: number; } | { [a: number]: Date; };
49+
>unionOfDifferentReturnType1 : { [x: number]: number; } | { [x: number]: Date; }
50+
>a : number
51+
>a : number
52+
>Date : Date
53+
54+
numOrDate = unionOfDifferentReturnType1["hello"]; // any
55+
>numOrDate = unionOfDifferentReturnType1["hello"] : any
56+
>numOrDate : number | Date
57+
>unionOfDifferentReturnType1["hello"] : any
58+
>unionOfDifferentReturnType1 : { [x: number]: number; } | { [x: number]: Date; }
59+
60+
numOrDate = unionOfDifferentReturnType1[10]; // number | Date
61+
>numOrDate = unionOfDifferentReturnType1[10] : number | Date
62+
>numOrDate : number | Date
63+
>unionOfDifferentReturnType1[10] : number | Date
64+
>unionOfDifferentReturnType1 : { [x: number]: number; } | { [x: number]: Date; }
65+
66+
var unionOfTypesWithAndWithoutStringSignature1: { [a: number]: number; } | boolean;
67+
>unionOfTypesWithAndWithoutStringSignature1 : boolean | { [x: number]: number; }
68+
>a : number
69+
70+
anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"]; // any
71+
>anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"] : any
72+
>anyVar : number
73+
>unionOfTypesWithAndWithoutStringSignature1["hello"] : any
74+
>unionOfTypesWithAndWithoutStringSignature1 : boolean | { [x: number]: number; }
75+
76+
anyVar = unionOfTypesWithAndWithoutStringSignature1[10]; // any
77+
>anyVar = unionOfTypesWithAndWithoutStringSignature1[10] : any
78+
>anyVar : number
79+
>unionOfTypesWithAndWithoutStringSignature1[10] : any
80+
>unionOfTypesWithAndWithoutStringSignature1 : boolean | { [x: number]: number; }
81+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var numOrDate: number | Date;
2+
var anyVar: number;
3+
4+
// If each type in U has a string index signature,
5+
// U has a string index signature of a union type of the types of the string index signatures from each type in U.
6+
7+
var unionOfDifferentReturnType: { [a: string]: number; } | { [a: string]: Date; };
8+
numOrDate = unionOfDifferentReturnType["hello"]; // number | Date
9+
numOrDate = unionOfDifferentReturnType[10]; // number | Date
10+
11+
var unionOfTypesWithAndWithoutStringSignature: { [a: string]: number; } | boolean;
12+
anyVar = unionOfTypesWithAndWithoutStringSignature["hello"]; // any
13+
anyVar = unionOfTypesWithAndWithoutStringSignature[10]; // any
14+
15+
// If each type in U has a numeric index signature,
16+
// U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
17+
var unionOfDifferentReturnType1: { [a: number]: number; } | { [a: number]: Date; };
18+
numOrDate = unionOfDifferentReturnType1["hello"]; // any
19+
numOrDate = unionOfDifferentReturnType1[10]; // number | Date
20+
21+
var unionOfTypesWithAndWithoutStringSignature1: { [a: number]: number; } | boolean;
22+
anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"]; // any
23+
anyVar = unionOfTypesWithAndWithoutStringSignature1[10]; // any

0 commit comments

Comments
 (0)