-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtypes.test.ts
196 lines (162 loc) · 8.4 KB
/
types.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
/* eslint-disable mmkal/@typescript-eslint/ban-ts-comment */
import * as a from '../src'
const {expectTypeOf} = a
test('boolean type logic', () => {
expectTypeOf<a.And<[true, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.And<[true, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.And<[true, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.And<[false, true]>>().toEqualTypeOf<false>()
expectTypeOf<a.And<[false, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.Or<[true, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.Or<[true, false]>>().toEqualTypeOf<true>()
expectTypeOf<a.Or<[false, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.Or<[false, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.Xor<[true, true]>>().toEqualTypeOf<false>()
expectTypeOf<a.Xor<[true, false]>>().toEqualTypeOf<true>()
expectTypeOf<a.Xor<[false, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.Xor<[false, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.Not<true>>().toEqualTypeOf<false>()
expectTypeOf<a.Not<false>>().toEqualTypeOf<true>()
expectTypeOf<a.IsAny<any>>().toEqualTypeOf<true>()
expectTypeOf<a.IsUnknown<any>>().toEqualTypeOf<false>()
expectTypeOf<a.IsNever<any>>().toEqualTypeOf<false>()
expectTypeOf<a.IsAny<unknown>>().toEqualTypeOf<false>()
expectTypeOf<a.IsUnknown<unknown>>().toEqualTypeOf<true>()
expectTypeOf<a.IsNever<unknown>>().toEqualTypeOf<false>()
expectTypeOf<a.IsAny<never>>().toEqualTypeOf<false>()
expectTypeOf<a.IsUnknown<never>>().toEqualTypeOf<false>()
expectTypeOf<a.IsNever<never>>().toEqualTypeOf<true>()
expectTypeOf<a.Extends<1, number>>().toEqualTypeOf<true>()
expectTypeOf<a.Extends<number, 1>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<1, 1>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<1, number>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<{a: 1}, {a: 1}>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<[{a: 1}], [{a: 1}]>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<never, never>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<any, any>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<unknown, unknown>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<any, never>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<any, unknown>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<never, unknown>>().toEqualTypeOf<false>()
})
test(`never types don't sneak by`, () => {
// @ts-expect-error
expectTypeOf<never>().toBeNumber()
// @ts-expect-error
expectTypeOf<never>().toBeString()
// @ts-expect-error
expectTypeOf<never>().toBeAny()
// @ts-expect-error
expectTypeOf<never>().toBeUnknown()
// @ts-expect-error
expectTypeOf<never>().toEqualTypeOf<{foo: string}>()
// @ts-expect-error
expectTypeOf<never>().toMatchTypeOf<{foo: string}>()
})
test('not cannot be chained', () => {
// @ts-expect-error
expectTypeOf<number>().not.not.toBeNumber()
})
test('constructor params', () => {
// The built-in ConstructorParameters type helper fails to pick up no-argument overloads.
// This test checks that's still the case to avoid unnecessarily maintaining a workaround,
// in case it's fixed in a future version
// broken built-in behaviour:
expectTypeOf<ConstructorParameters<typeof Date>>().toEqualTypeOf<[string | number | Date]>()
expectTypeOf<typeof Date extends new (...args: infer Args) => any ? Args : never>().toEqualTypeOf<
[string | number | Date]
>()
// workaround:
expectTypeOf<a.ConstructorParams<typeof Date>>().toEqualTypeOf<[] | [string | number | Date]>()
})
test('guarded & asserted types', () => {
expectTypeOf<(v: any) => v is string>().guards.toBeString()
expectTypeOf<(v: any) => asserts v is number>().asserts.toBeNumber()
// @ts-expect-error
expectTypeOf<(v: any) => boolean>().guards.toBeAny()
// @ts-expect-error
expectTypeOf<(v: any) => boolean>().asserts.toBeAny()
})
test('parity with IsExact from conditional-type-checks', () => {
// lifted from https://github.com/dsherret/conditional-type-checks/blob/01215056e8b97a28c5b0311b42ed48c70c8723fe/tests.ts#L18-L63
// there are some redundant type constituents, but dont' fix them because they came from above and it'll make updating harder
/* eslint-disable mmkal/@typescript-eslint/no-redundant-type-constituents */
/** shim conditional-type-check's `assert` */
const assert = <T extends boolean>(_result: T) => true
/** shim conditional-type-check's `IsExact` using `Equal` */
type IsExact<T, U> = a.Equal<T, U>
// basic test for `assert` shim:
expectTypeOf(assert).toBeCallableWith(true)
expectTypeOf(assert).toBeCallableWith(false)
expectTypeOf(assert).returns.toBeBoolean()
// basic test for `IsExact` shim:
expectTypeOf<IsExact<0, 0>>().toEqualTypeOf<true>()
expectTypeOf<IsExact<0, 1>>().toEqualTypeOf<false>()
// test for false negatives in shims:
// @ts-expect-error
expectTypeOf<IsExact<0, 0>>().toEqualTypeOf<false>()
// @ts-expect-error
expectTypeOf<IsExact<0, 1>>().toEqualTypeOf<true>()
// @ts-expect-error
assert<IsExact<0, 0>>(false)
// @ts-expect-error
assert<IsExact<0, 1>>(true)
// conditional-type-check `IsExact` tests, copy-pasted directly:
// matching
assert<IsExact<string | number, string | number>>(true)
assert<IsExact<string | number | Date, string | number | Date>>(true)
assert<IsExact<string | undefined, string | undefined>>(true)
assert<IsExact<any, any>>(true) // ok to have any for both
assert<IsExact<unknown, unknown>>(true)
assert<IsExact<never, never>>(true)
assert<IsExact<{}, {}>>(true)
assert<IsExact<{prop: string}, {prop: string}>>(true)
assert<IsExact<{prop: {prop: string}}, {prop: {prop: string}}>>(true)
assert<IsExact<{prop: never}, {prop: never}>>(true)
assert<IsExact<{prop: any}, {prop: any}>>(true)
assert<IsExact<{prop: unknown}, {prop: unknown}>>(true)
assert<IsExact<Window, Window>>(true)
// not matching
assert<IsExact<string | number | Date, string | number>>(false)
assert<IsExact<string, string | number>>(false)
assert<IsExact<string | undefined, string>>(false)
assert<IsExact<string | undefined, any | string>>(false)
assert<IsExact<any | string | undefined, string>>(false)
assert<IsExact<string, any>>(false)
assert<IsExact<string, unknown>>(false)
assert<IsExact<string, never>>(false)
assert<IsExact<never, never | string>>(false)
assert<IsExact<unknown, any>>(false)
assert<IsExact<never, any>>(false)
assert<IsExact<MouseEvent | Window, MouseEvent>>(false)
assert<IsExact<{name: string; other?: Date}, {name: string}>>(false)
assert<IsExact<{prop: Date}, {prop: string}>>(false)
assert<IsExact<{other?: Date}, {prop?: string}>>(false)
assert<IsExact<{prop: {prop?: string}}, {prop: {prop: string}}>>(false)
assert<IsExact<{prop: any}, {prop: string}>>(false)
assert<IsExact<{prop: any}, {prop: unknown}>>(false)
assert<IsExact<{prop: any}, {prop: never}>>(false)
assert<IsExact<{prop: unknown}, {prop: never}>>(false)
assert<IsExact<{prop: {prop: unknown}}, {prop: {prop: any}}>>(false)
assert<IsExact<{prop: {prop: unknown}}, {prop: {prop: never}}>>(false)
assert<IsExact<{prop: {prop: any}}, {prop: {prop: never}}>>(false)
assert<IsExact<{prop: string}, {prop: never}>>(false)
assert<IsExact<{prop: {prop: any}}, {prop: {prop: string}}>>(false)
assert<IsExact<{prop: any} | {prop: string}, {prop: number} | {prop: string}>>(false)
assert<IsExact<{prop: string | undefined}, {prop?: string}>>(false) // these are different
})
test('Equal works with functions', () => {
expectTypeOf<a.Equal<() => void, () => string>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<() => void, (s: string) => void>>().toEqualTypeOf<false>()
// todo: workaround https://github.com/microsoft/TypeScript/issues/50670 - https://github.com/mmkal/expect-type/issues/5
// expectTypeOf<a.Equal<() => () => () => void, () => () => () => string>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<() => () => () => void, () => (s: string) => () => void>>().toEqualTypeOf<false>()
})
test(`undefined isn't removed from unions`, () => {
expectTypeOf<string | null | undefined>().toEqualTypeOf('' as string | null | undefined)
expectTypeOf<string | null | undefined>().toMatchTypeOf('' as string | null | undefined)
expectTypeOf('' as string | null | undefined).toEqualTypeOf<string | null | undefined>()
expectTypeOf('' as string | null | undefined).toMatchTypeOf<string | null | undefined>()
expectTypeOf<string | null | undefined>().toEqualTypeOf<string | null | undefined>()
expectTypeOf<string | null | undefined>().toMatchTypeOf<string | null | undefined>()
})