-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshim.test.ts
317 lines (247 loc) · 7.57 KB
/
shim.test.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { Equal, Expect } from './utils'
// Test Object.hasOwn()
{
const objUnion = { a: 12 } as ({ a: number } | { b: number })
// key on Object.prototype
if (Object.hasOwn(objUnion, 'toString')) {
type Test = Expect<Equal<typeof objUnion, { a: number } | { b: number }>>
} else {
type Test = Expect<Equal<typeof objUnion, { a: number } | { b: number }>>
}
// key of union, narrow type
if (Object.hasOwn(objUnion, 'a')) {
type Test = Expect<Equal<typeof objUnion, { a: number }>>
} else {
type Test = Expect<Equal<typeof objUnion, { b: number }>>
}
// if not key, don't narrow
if (Object.hasOwn(objUnion, 'c')) {
type Test = Expect<Equal<typeof objUnion, ({ a: number } | { b: number }) & { c: unknown }>>
} else {
type Test = Expect<Equal<typeof objUnion, { a: number } | { b: number }>>
}
}
// Test Reflect.has()
{
const objUnion = { a: 12 } as { a: number } | { b: number }
// if key on Object.prototype
if (Reflect.has(objUnion, 'toString')) {
type Test = Expect<Equal<typeof objUnion, { a: number } | { b: number }>>
} else {
type Test = Expect<Equal<typeof objUnion, never>>
}
// key of union
if (Reflect.has(objUnion, 'a')) {
type Test = Expect<Equal<typeof objUnion, { a: number }>>
} else {
type Test = Expect<Equal<typeof objUnion, { b: number }>>
}
// not key, do nothing
if (Reflect.has(objUnion, 'c')) {
type Test = Expect<Equal<typeof objUnion, { a: number } | { b: number }>>
} else {
type Test = Expect<Equal<typeof objUnion, { a: number } | { b: number }>>
}
}
// Test Object.entries()
{
{
// preserve key type,
const objectEntries = Object.entries({ a: 12, [Symbol()]: Symbol(), 12: '33' })
const arrayEntries = Object.entries([12, 34])
type TestCase = [
Expect<Equal<typeof objectEntries, ['a' | '12', number | string][]>>,
Expect<Equal<typeof arrayEntries, [`${number}`, number][]>>
]
}
{
// primitive types
const entriesOfNumber = Object.entries(0)
const entriesOfString = Object.entries('123')
const entriesOfSymbol = Object.entries(Symbol())
type TestCase = [
Expect<Equal<typeof entriesOfNumber, []>>,
Expect<Equal<typeof entriesOfString, [`${number}`, string][]>>,
Expect<Equal<typeof entriesOfSymbol, []>>
]
}
}
// Test Object.fromEntrires()
{
{
// preserve key type,
type KeyType = 'a' | 'b' | 'c'
const sameValueType = Object.fromEntries([
['a', 12],
['b', 90],
['c', 92]
])
const diffValueType = Object.fromEntries([
['a', 12],
['b', '90'],
['c', Symbol()]
])
type TestCase = [
Expect<Equal<typeof sameValueType, { [k in KeyType]: number }>>,
Expect<Equal<typeof diffValueType, { [k in KeyType]: number | string | symbol }>>
]
}
}
// Test Object.keys()
{
{
// symbol key ignored
const keys = Object.keys({ str: 12, 12: 99, [Symbol()]: 123 })
type Test = Expect<Equal<typeof keys, ('str' | '12')[]>>
}
{
// primitive types
const keysOfNumber = Object.keys(0)
const keysOfString = Object.keys('123')
const keysOfSymbol = Object.keys(Symbol())
type TestCase = [
Expect<Equal<typeof keysOfNumber, []>>,
Expect<Equal<typeof keysOfString, `${number}`[]>>,
Expect<Equal<typeof keysOfSymbol, []>>
]
}
}
// Test Object.values()
{
{
// symbol key value ignored
const values = Object.values({ str: 12, 12: 99, [Symbol()]: 123 })
type Test = Expect<Equal<typeof values, number[]>>
}
{
// primitive types
const valuesOfNumber = Object.values(0)
const valuesOfString = Object.values('123')
const valuesOfSymbol = Object.values(Symbol())
type TestCase = [
Expect<Equal<typeof valuesOfNumber, []>>,
Expect<Equal<typeof valuesOfString, ['1', '2', '3']>>,
Expect<Equal<typeof valuesOfSymbol, []>>
]
}
}
// Test `Reflect.get`
{
{
const obj = { a: 12 }
const val = Reflect.get(obj, 'a')
type TestCase = [
Expect<Equal<typeof val, number>>
]
}
}
// Test `Object.prototype.toString`
{
const a = {}
const f = function () {
}
if (Object.prototype.toString.call(f) === '[object Function]') {
f()
} else {
f()
}
}
// Test `Array.prototype.includes`
{
const a: ('a' | 'b')[] = []
const c = '' as string
if (a.includes(c)) {
type TestCase = Expect<Equal<(typeof a)[number], typeof c>>
} else {
type TestCase = Expect<Equal<string, typeof c>>
}
}
// Test document.getElementById
{
const el = document.getElementById<HTMLDivElement>('123')
const frag = document.createDocumentFragment()
const gl = frag.getElementById<HTMLDivElement>('123')
type TestCase = [
Expect<Equal<typeof el, HTMLDivElement | null>>,
Expect<Equal<typeof gl, HTMLDivElement | null>>
]
}
// Test `Array.prototype.concat`
// {
// const a = [1, 2, 3]
// const b = ['1', '2', '3']
// type A = typeof a
// type B = typeof b
// const g = a.concat(a)
// const c = a.concat(b, '', '123', ['123'])
// // many types?
// // const d = a.concat(b, [true])
// const n = [{ a: '123', b: 123, c: true }]
// type TestCase = [Expect<Equal<typeof g, A>>,
// Expect<Equal<typeof c, (A[number] | B[number])[]>>
// ]
// }
// Test `Array.isArray`
{
const testConst = [1, 2, 3] as const
if (Array.isArray(testConst)) {
// presever readonly array type
type TestCase = Expect<Equal<typeof testConst, readonly [1, 2, 3]>>
} else {
type TestCase = Expect<Equal<typeof testConst, never>>
}
const testReadonlyArrayUnion = [] as readonly number[] | number
if (Array.isArray(testReadonlyArrayUnion)) {
// narrow to ReadonlyArray<number> instead of any[]
type TestCase = Expect<Equal<typeof testReadonlyArrayUnion, ReadonlyArray<number>>>
} else {
type TestCase = Expect<Equal<typeof testReadonlyArrayUnion, number>>
}
const testArr = [1, 2, 3]
if (Array.isArray(testArr)) {
type TestCase = Expect<Equal<typeof testArr, number[]>>
} else {
type TestCase = Expect<Equal<typeof testArr, never>>
}
const testNormalUnion = '123' as string | string[]
if (Array.isArray(testNormalUnion)) {
type TestCase = Expect<Equal<typeof testNormalUnion, string[]>>
} else {
type TestCase = Expect<Equal<typeof testNormalUnion, string>>
}
const testAny = '' as any
if (Array.isArray(testAny)) {
// narrow any to unknown[] instead of any[]
type TestCase = Expect<Equal<typeof testAny, unknown[]>>
} else {
type TestCase = Expect<Equal<typeof testAny, any>>
}
const testUnknown = '' as unknown
if (Array.isArray(testUnknown)) {
// narrow unknown to unknown[] instead of any[]
type TestCase = Expect<Equal<typeof testUnknown, unknown[]>>
} else {
type TestCase = Expect<Equal<typeof testUnknown, unknown>>
}
const testNotArr = '' as string | number
if (Array.isArray(testNotArr)) {
// narrow to never if not array
type TestCase = Expect<Equal<typeof testNotArr, never>>
} else {
type TestCase = Expect<Equal<typeof testNotArr, string | number>>
}
class TestSubClass extends Array { }
const testSubClass = new TestSubClass()
if (Array.isArray(testSubClass)) {
type TestCase = Expect<Equal<typeof testSubClass, TestSubClass>>
} else {
type TestCase = Expect<Equal<typeof testSubClass, never>>
}
const testNodeList = [] as unknown as NodeListOf<ChildNode>
if (Array.isArray(testNodeList)) {
type TestCase = Expect<Equal<typeof testNodeList, never>>
} else {
type TestCase = Expect<Equal<typeof testNodeList, NodeListOf<ChildNode>>>
}
}