Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import type { DistributiveOmit, OmitKeyof } from '..'
3β
4describe('OmitKeyof', () => {
5 it('should omit a string key and reject one the object does not have', () => {
6 type A = {
7 x: string
8 y: number
9 }
10β
11 type ExpectedType = {
12 x: string
13 }
14β
15 // Bad point
16 // 1. original Omit can use 'z' as type parameter with no type error
17 // 2. original Omit have no auto complete for 2nd type parameter
18 expectTypeOf<Omit<A, 'z' | 'y'>>().toEqualTypeOf<ExpectedType>()
19β
20 // Solution
21β
22 // 1. strictly
23 expectTypeOf<
24 OmitKeyof<
25 A,
26 // OmitKeyof can't use 'z' as type parameter with type error because A don't have key 'z'
27 // @ts-expect-error Type does not satisfy the constraint keyof A
28 'z' | 'y'
29 >
30 >().toEqualTypeOf<ExpectedType>()
31 expectTypeOf<
32 OmitKeyof<
33 A,
34 // OmitKeyof can't use 'z' as type parameter with type error because A don't have key 'z'
35 // @ts-expect-error Type does not satisfy the constraint keyof A
36 'z' | 'y',
37 'strictly'
38 >
39 >().toEqualTypeOf<ExpectedType>()
40β
41 // 2. safely
42 expectTypeOf<
43 OmitKeyof<
44 A,
45 // OmitKeyof can't use 'z' as type parameter type error with strictly parameter or default parameter
46 // @ts-expect-error Type does not satisfy the constraint keyof A
47 'z' | 'y'
48 >
49 >().toEqualTypeOf<ExpectedType>()
50 expectTypeOf<
51 OmitKeyof<
52 A,
53 // With 'safely', OmitKeyof can use 'z' as type parameter like original Omit but This support autocomplete too yet for DX.
54 'z' | 'y',
55 'safely'
56 >
57 >().toEqualTypeOf<ExpectedType>()
58 })
59β
60 it('should omit a number key and reject one the object does not have', () => {
61 type A = {
62 [1]: string
63 [2]: number
64 }
65β
66 type ExpectedType = {
67 [1]: string
68 }
69β
70 // Bad point
71 // 1. original Omit can use 3 as type parameter with no type error
72 // 2. original Omit have no auto complete for 2nd type parameter
73 expectTypeOf<Omit<A, 3 | 2>>().toEqualTypeOf<ExpectedType>()
74β
75 // Solution
76β
77 // 1. strictly
78 expectTypeOf<
79 OmitKeyof<
80 A,
81 // OmitKeyof can't use 3 as type parameter with type error because A don't have key 3
82 // @ts-expect-error Type does not satisfy the constraint keyof A
83 3 | 2
84 >
85 >().toEqualTypeOf<ExpectedType>()
86 expectTypeOf<
87 OmitKeyof<
88 A,
89 // OmitKeyof can't use 3 as type parameter with type error because A don't have key 3
90 // @ts-expect-error Type does not satisfy the constraint keyof A
91 3 | 2,
92 'strictly'
93 >
94 >().toEqualTypeOf<ExpectedType>()
95β
96 // 2. safely
97 expectTypeOf<
98 OmitKeyof<
99 A,
100 // OmitKeyof can't use 3 as type parameter type error with strictly parameter or default parameter
101 // @ts-expect-error Type does not satisfy the constraint keyof A
102 3 | 2
103 >
104 >().toEqualTypeOf<ExpectedType>()
105 expectTypeOf<
106 OmitKeyof<
107 A,
108 // With 'safely', OmitKeyof can use 3 as type parameter like original Omit but This support autocomplete too yet for DX.
109 3 | 2,
110 'safely'
111 >
112 >().toEqualTypeOf<ExpectedType>()
113 })
114β
115 it('should omit a symbol key and reject one the object does not have', () => {
116 const symbol1 = Symbol()
117 const symbol2 = Symbol()
118 const symbol3 = Symbol()
119β
120 type A = {
121 [symbol1]: string
122 [symbol2]: number
123 }
124β
125 type ExpectedType = {
126 [symbol1]: string
127 }
128β
129 // Bad point
130 // 1. original Omit can use symbol3 as type parameter with no type error
131 // 2. original Omit have no auto complete for 2nd type parameter
132 expectTypeOf<
133 Omit<A, typeof symbol3 | typeof symbol2>
134 >().toEqualTypeOf<ExpectedType>()
135β
136 // Solution
137β
138 // 1. strictly
139 expectTypeOf<
140 OmitKeyof<
141 A,
142 // OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3
143 // @ts-expect-error Type does not satisfy the constraint keyof A
144 typeof symbol3 | typeof symbol2
145 >
146 >().toEqualTypeOf<ExpectedType>()
147 expectTypeOf<
148 OmitKeyof<
149 A,
150 // OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3
151 // @ts-expect-error Type does not satisfy the constraint keyof A
152 typeof symbol3 | typeof symbol2,
153 'strictly'
154 >
155 >().toEqualTypeOf<ExpectedType>()
156β
157 // 2. safely
158 expectTypeOf<
159 OmitKeyof<
160 A,
161 // OmitKeyof can't use symbol3 as type parameter type error with strictly parameter or default parameter
162 // @ts-expect-error Type does not satisfy the constraint keyof A
163 typeof symbol3 | typeof symbol2
164 >
165 >().toEqualTypeOf<ExpectedType>()
166 expectTypeOf<
167 OmitKeyof<
168 A,
169 // With 'safely', OmitKeyof can use symbol3 as type parameter like original Omit but This support autocomplete too yet for DX.
170 typeof symbol3 | typeof symbol2,
171 'safely'
172 >
173 >().toEqualTypeOf<ExpectedType>()
174 })
175β
176 it('should default TStrictly to strictly', () => {
177 type A = {
178 x: string
179 y: number
180 }
181β
182 // the two-argument form must behave exactly like the explicit 'strictly' form
183 expectTypeOf<OmitKeyof<A, 'y'>>().toEqualTypeOf<
184 OmitKeyof<A, 'y', 'strictly'>
185 >()
186β
187 // and it must reject an unknown key just like 'strictly' does
188 expectTypeOf<
189 OmitKeyof<
190 A,
191 // @ts-expect-error Type does not satisfy the constraint keyof A
192 'z'
193 >
194 >().toEqualTypeOf<A>()
195 })
196β
197 it('should constrain TStrictly to strictly or safely', () => {
198 type A = {
199 x: string
200 y: number
201 }
202β
203 expectTypeOf<OmitKeyof<A, 'y', 'safely'>>().toEqualTypeOf<{ x: string }>()
204β
205 expectTypeOf<
206 OmitKeyof<
207 A,
208 'y',
209 // @ts-expect-error 'loosely' is not a valid TStrictly
210 'loosely'
211 >
212 >().toEqualTypeOf<{ x: string }>()
213 })
214β
215 it('should accept an arbitrary key only with safely, never with strictly', () => {
216 type A = {
217 x: string
218 y: number
219 }
220β
221 // 'safely' widens the constraint so an unrelated key is accepted and simply ignored
222 expectTypeOf<OmitKeyof<A, 'z', 'safely'>>().toEqualTypeOf<A>()
223 expectTypeOf<OmitKeyof<A, 3, 'safely'>>().toEqualTypeOf<A>()
224β
225 // while 'strictly' rejects the very same key
226 expectTypeOf<
227 OmitKeyof<
228 A,
229 // @ts-expect-error Type does not satisfy the constraint keyof A
230 'z',
231 'strictly'
232 >
233 >().toEqualTypeOf<A>()
234 })
235β
236 it('should preserve optional and readonly modifiers', () => {
237 type A = {
238 readonly x: string
239 y?: number
240 readonly z?: boolean
241 }
242β
243 expectTypeOf<OmitKeyof<A, 'y'>>().toEqualTypeOf<{
244 readonly x: string
245 readonly z?: boolean
246 }>()
247β
248 expectTypeOf<OmitKeyof<A, 'z'>>().toEqualTypeOf<{
249 readonly x: string
250 y?: number
251 }>()
252 })
253β
254 it('should collapse a union instead of distributing over it', () => {
255 type A = { kind: 'a'; shared: string; onlyA: number }
256 type B = { kind: 'b'; shared: string; onlyB: boolean }
257β
258 // `keyof (A | B)` is the intersection of the keys, so only the common keys
259 // satisfy the 'strictly' constraint
260 expectTypeOf<OmitKeyof<A | B, 'shared'>>().toEqualTypeOf<{
261 kind: 'a' | 'b'
262 }>()
263β
264 // a key that only exists on one member is rejected by 'strictly'
265 expectTypeOf<
266 OmitKeyof<
267 A | B,
268 // @ts-expect-error 'onlyA' is not in keyof (A | B)
269 'onlyA'
270 >
271 >().toEqualTypeOf<{ kind: 'a' | 'b'; shared: string }>()
272 })
273β
274 it('should accept every key of an index signature', () => {
275 type A = Record<string, number>
276β
277 // `keyof Record<string, number>` is `string | number`, so 'strictly'
278 // accepts an arbitrary key here even though nothing is removed
279 expectTypeOf<OmitKeyof<A, 'anything'>>().toEqualTypeOf<{
280 [x: string]: number
281 }>()
282β
283 expectTypeOf<OmitKeyof<A, string>>().toEqualTypeOf<{}>()
284 })
285β
286 it('should omit every key when TKey is keyof TObject', () => {
287 type A = {
288 x: string
289 y: number
290 }
291β
292 expectTypeOf<OmitKeyof<A, keyof A>>().toEqualTypeOf<{}>()
293 expectTypeOf<OmitKeyof<A, never>>().toEqualTypeOf<A>()
294 })
295})
296β
297describe('DistributiveOmit', () => {
298 it('should distribute over a union instead of collapsing it, unlike OmitKeyof', () => {
299 type A = { kind: 'a'; shared: string; onlyA: number }
300 type B = { kind: 'b'; shared: string; onlyB: boolean }
301β
302 // each union member is omitted from separately, so the member-only keys survive
303 expectTypeOf<DistributiveOmit<A | B, 'shared'>>().toEqualTypeOf<
304 { kind: 'a'; onlyA: number } | { kind: 'b'; onlyB: boolean }
305 >()
306β
307 // whereas OmitKeyof collapses the union into its common keys
308 expectTypeOf<OmitKeyof<A | B, 'shared'>>().toEqualTypeOf<{
309 kind: 'a' | 'b'
310 }>()
311 })
312β
313 it('should constrain TKey to the keys shared by every union member', () => {
314 type A = { kind: 'a'; shared: string; onlyA: number }
315 type B = { kind: 'b'; shared: string; onlyB: boolean }
316β
317 // a key that only exists on one member is rejected, because the constraint
318 // is `keyof TObject` on the union itself rather than on each distributed arm
319 expectTypeOf<
320 DistributiveOmit<
321 A | B,
322 // @ts-expect-error 'onlyA' is not in keyof (A | B)
323 'onlyA'
324 >['kind']
325 >().toEqualTypeOf<'a' | 'b'>()
326β
327 // the key shared by every member is accepted
328 expectTypeOf<DistributiveOmit<A | B, 'kind'>>().toEqualTypeOf<
329 { shared: string; onlyA: number } | { shared: string; onlyB: boolean }
330 >()
331 })
332β
333 it('should behave like Omit on a non-union object', () => {
334 type A = {
335 x: string
336 y: number
337 }
338β
339 expectTypeOf<DistributiveOmit<A, 'y'>>().toEqualTypeOf<{ x: string }>()
340 })
341})