forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputedPropertyUnionLiftsToUnionType.js
More file actions
127 lines (115 loc) · 3.23 KB
/
computedPropertyUnionLiftsToUnionType.js
File metadata and controls
127 lines (115 loc) · 3.23 KB
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
125
126
127
//// [tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts] ////
//// [computedPropertyUnionLiftsToUnionType.ts]
declare var ab: 'a' | 'b';
declare var cd: 'c' | 'd';
declare var onetwo: 1 | 2;
enum Alphabet {
Aleph,
Bet,
}
declare var alphabet: Alphabet;
// Basic: union literal key lifts to union of object types
const x: { a: string } | { b: string } = { [ab]: 'hi' }
// Multiple unions create cross-product
const y: { a: string, m: number, c: string }
| { a: string, m: number, d: string }
| { b: string, m: number, c: string }
| { b: string, m: number, d: string } = { [ab]: 'hi', m: 1, [cd]: 'there' }
// Union + spread
const s: { a: string, c: string } | { b: string, c: string } = { [ab]: 'hi', ...{ c: 'no' }}
// Number literal union
const n: { "1": string } | { "2": string } = { [onetwo]: 'hi' }
// Enum literal union
const e: { "0": string } | { "1": string } = { [alphabet]: 'hi' }
// Soundness check: accessing non-existent property should be error
const obj = { [ab]: 1 }
// obj should be { a: number } | { b: number }, not { a: number; b: number }
// So accessing both .a and .b should require a type guard
// Methods and getters alongside union computed property
const m: { a: string, m(): number, p: number } | { b: string, m(): number, p: number } =
{ [ab]: 'hi', m() { return 1 }, get p() { return 2 } }
//// [computedPropertyUnionLiftsToUnionType.js]
"use strict";
var Alphabet;
(function (Alphabet) {
Alphabet[Alphabet["Aleph"] = 0] = "Aleph";
Alphabet[Alphabet["Bet"] = 1] = "Bet";
})(Alphabet || (Alphabet = {}));
// Basic: union literal key lifts to union of object types
const x = { [ab]: 'hi' };
// Multiple unions create cross-product
const y = { [ab]: 'hi', m: 1, [cd]: 'there' };
// Union + spread
const s = Object.assign({ [ab]: 'hi' }, { c: 'no' });
// Number literal union
const n = { [onetwo]: 'hi' };
// Enum literal union
const e = { [alphabet]: 'hi' };
// Soundness check: accessing non-existent property should be error
const obj = { [ab]: 1 };
// obj should be { a: number } | { b: number }, not { a: number; b: number }
// So accessing both .a and .b should require a type guard
// Methods and getters alongside union computed property
const m = { [ab]: 'hi', m() { return 1; }, get p() { return 2; } };
//// [computedPropertyUnionLiftsToUnionType.d.ts]
declare var ab: 'a' | 'b';
declare var cd: 'c' | 'd';
declare var onetwo: 1 | 2;
declare enum Alphabet {
Aleph = 0,
Bet = 1
}
declare var alphabet: Alphabet;
declare const x: {
a: string;
} | {
b: string;
};
declare const y: {
a: string;
m: number;
c: string;
} | {
a: string;
m: number;
d: string;
} | {
b: string;
m: number;
c: string;
} | {
b: string;
m: number;
d: string;
};
declare const s: {
a: string;
c: string;
} | {
b: string;
c: string;
};
declare const n: {
"1": string;
} | {
"2": string;
};
declare const e: {
"0": string;
} | {
"1": string;
};
declare const obj: {
a: number;
} | {
b: number;
};
declare const m: {
a: string;
m(): number;
p: number;
} | {
b: string;
m(): number;
p: number;
};