Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import type {
3 DeepKeys,
4 DeepKeysWhereValueIncludes,
5 DeepValue,
6 TryGetArrayElementType,
7} from '../src/index'
8β
9interface User {
10 name: string
11 id: string
12 age: number
13}
14β
15// TODO we should give people a way to narrow things for themselves.
16// For example, if they have a field of Foo | Bar and they do a runtime check of Foo,
17// it would be nice if Field or Form can be cast in a way where they pick up on it.
18// How, you ask? Idfk, go figure it out.
19β
20// TODO this PR changes DeepKeysAndValues. Add its regression spec before v2.
21// https://github.com/TanStack/form/pull/2057
22β
23/**
24 * Properly recognizes that `0` is not an object and should not have subkeys
25 */
26describe('DeepKeys', () => {
27 it('Should support tuples with various internal types', () => {
28 type TestShape = { topUsers: [User, 0, User] }
29 // It should not use template `number` since it's a tuple
30 type Expected =
31 | 'topUsers'
32 | 'topUsers[0]'
33 | 'topUsers[0].name'
34 | 'topUsers[0].id'
35 | 'topUsers[0].age'
36 | 'topUsers[1]'
37 | 'topUsers[2]'
38 | 'topUsers[2].name'
39 | 'topUsers[2].id'
40 | 'topUsers[2].age'
41β
42 expectTypeOf<DeepKeys<TestShape>>().toEqualTypeOf<Expected>()
43 })
44β
45 it('Should support arrays', () => {
46 type OneElementType = { users: Array<User> }
47 type ExpectedOneElement =
48 | 'users'
49 | `users[${number}]`
50 | `users[${number}].name`
51 | `users[${number}].id`
52 | `users[${number}].age`
53β
54 expectTypeOf<DeepKeys<OneElementType>>().toEqualTypeOf<ExpectedOneElement>()
55 })
56β
57 it('Should allow keys for unions in arrays', () => {
58 type UnionType = { users: Array<User | number> }
59 type ExpectedUnionType =
60 | 'users'
61 | `users[${number}]`
62 | `users[${number}].name`
63 | `users[${number}].id`
64 | `users[${number}].age`
65β
66 expectTypeOf<DeepKeys<UnionType>>().toEqualTypeOf<ExpectedUnionType>()
67 })
68β
69 it('Should allow deep nesting', () => {
70 type NestedSupport = { meta: { mainUser: User } }
71 type ExpectedKeys =
72 | 'meta'
73 | 'meta.mainUser'
74 | 'meta.mainUser.name'
75 | 'meta.mainUser.id'
76 | 'meta.mainUser.age'
77β
78 expectTypeOf<DeepKeys<NestedSupport>>().toEqualTypeOf<ExpectedKeys>()
79 })
80β
81 it('Should allow partial objects', () => {
82 type PartialObject = { meta?: { mainUser?: User } }
83 type ExpectedKeys =
84 | 'meta'
85 | 'meta.mainUser'
86 | 'meta.mainUser.name'
87 | 'meta.mainUser.id'
88 | 'meta.mainUser.age'
89β
90 expectTypeOf<DeepKeys<PartialObject>>().toEqualTypeOf<ExpectedKeys>()
91 })
92β
93 it('should handle the `object` type', () => {
94 expectTypeOf<DeepKeys<object>>().toEqualTypeOf<string>()
95β
96 type NestedObject = { meta: { mainUser: object } }
97 type ExpectedKeys = 'meta' | 'meta.mainUser' | `meta.mainUser.${string}`
98β
99 expectTypeOf<DeepKeys<NestedObject>>().toEqualTypeOf<ExpectedKeys>()
100 })
101β
102 it('should handle `unknown` in types', () => {
103 expectTypeOf<DeepKeys<unknown>>().toEqualTypeOf<string>()
104β
105 type NestedUnknown = { meta: { mainUser: unknown } }
106 type ExpectedKeys = 'meta' | 'meta.mainUser' | `meta.mainUser.${string}`
107β
108 expectTypeOf<DeepKeys<NestedUnknown>>().toEqualTypeOf<ExpectedKeys>()
109 })
110β
111 it('should handle `any` in types', () => {
112 expectTypeOf<DeepKeys<any>>().toEqualTypeOf<string>()
113β
114 type NestedUnknown = { meta: { mainUser: any } }
115 type ExpectedKeys = 'meta' | 'meta.mainUser' | `meta.mainUser.${string}`
116β
117 expectTypeOf<DeepKeys<NestedUnknown>>().toEqualTypeOf<ExpectedKeys>()
118 })
119β
120 it('should handle unions and intersections', () => {
121 type DiscriminatedUnion = { name: string } & (
122 { variant: 'foo' } | { variant: 'bar'; baz: boolean }
123 )
124 type ExpectedKeys = 'name' | 'variant' | 'baz'
125β
126 expectTypeOf<DeepKeys<DiscriminatedUnion>>().toEqualTypeOf<ExpectedKeys>()
127 })
128β
129 it('should handle nullish states', () => {
130 type Insert = { mainUser: 'name' }
131 type NullishStates = {
132 required: Insert
133 nullable: Insert | null
134 undefinable: Insert | undefined
135 optional?: Insert
136 mixed: Insert | null | undefined
137 onlyNull: null
138 onlyUndefined: undefined
139 optionalNull?: null
140 onlyNil: null | undefined
141 }
142β
143 type ExpectedKeys =
144 | 'required'
145 | 'required.mainUser'
146 | 'nullable'
147 | 'nullable.mainUser'
148 | 'undefinable'
149 | 'undefinable.mainUser'
150 | 'optional'
151 | 'optional.mainUser'
152 | 'mixed'
153 | 'mixed.mainUser'
154 | 'onlyNull'
155 | 'onlyUndefined'
156 | 'optionalNull'
157 | 'onlyNil'
158β
159 expectTypeOf<DeepKeys<NullishStates>>().toEqualTypeOf<ExpectedKeys>()
160 })
161β
162 it('should handle nullish states with nested objects', () => {
163 type NestedNullishState = {
164 mixed?: { mainUser: { name: 'name' } } | null | undefined
165 }
166β
167 type ExpectedKeys = 'mixed' | 'mixed.mainUser' | 'mixed.mainUser.name'
168β
169 expectTypeOf<DeepKeys<NestedNullishState>>().toEqualTypeOf<ExpectedKeys>()
170 })
171β
172 it('should handle unions with nested cases', () => {
173 type TheUnion =
174 | { a: User }
175 | { a: string }
176 | { b: string }
177 | { c: { user: User } | { user: number } }
178 type NestedObjectUnionCase = {
179 normal: TheUnion
180 }
181 type ExpectedKeys =
182 | 'normal'
183 | 'normal.a'
184 | 'normal.a.name'
185 | 'normal.a.id'
186 | 'normal.a.age'
187 | 'normal.b'
188 | 'normal.c'
189 | 'normal.c.user'
190 | 'normal.c.user.name'
191 | 'normal.c.user.id'
192 | 'normal.c.user.age'
193β
194 expectTypeOf<
195 DeepKeys<NestedObjectUnionCase>
196 >().toEqualTypeOf<ExpectedKeys>()
197 })
198β
199 it('should handle nullish within nested unions', () => {
200 type NullishNested = {
201 nullable:
202 | { a?: number; b?: { c: boolean } | null }
203 | { b?: { c: string; e: number } }
204 }
205β
206 type ExpectedKeys =
207 'nullable' | 'nullable.a' | 'nullable.b' | 'nullable.b.c' | 'nullable.b.e'
208β
209 expectTypeOf<DeepKeys<NullishNested>>().toEqualTypeOf<ExpectedKeys>()
210 })
211β
212 it('should handle one dimensional arrays', () => {
213 type OneDimArrays = {
214 ages: Array<number>
215 names: Array<string>
216 users: Array<User>
217 }
218 type ExpectedKeys =
219 | 'ages'
220 | `ages[${number}]`
221 | 'names'
222 | `names[${number}]`
223 | 'users'
224 | `users[${number}]`
225 | `users[${number}].name`
226 | `users[${number}].id`
227 | `users[${number}].age`
228β
229 expectTypeOf<DeepKeys<OneDimArrays>>().toEqualTypeOf<ExpectedKeys>()
230 })
231β
232 it('should handle two dimensional arrays', () => {
233 type TwoDimArrays = {
234 ages: Array<Array<number>>
235 names: Array<Array<string>>
236 users: Array<Array<User>>
237 }
238 type ExpectedKeys =
239 | 'ages'
240 | `ages[${number}]`
241 | `ages[${number}][${number}]`
242 | 'names'
243 | `names[${number}]`
244 | `names[${number}][${number}]`
245 | 'users'
246 | `users[${number}]`
247 | `users[${number}][${number}]`
248 | `users[${number}][${number}].name`
249 | `users[${number}][${number}].id`
250 | `users[${number}][${number}].age`
251β
252 expectTypeOf<DeepKeys<TwoDimArrays>>().toEqualTypeOf<ExpectedKeys>()
253 })
254β
255 it('should handle two arrays with object', () => {
256 type TwoDimArrays = {
257 entries: Array<{
258 ages: Array<number>
259 names: Array<string>
260 users: Array<User>
261 }>
262 }
263 type ExpectedKeys =
264 | 'entries'
265 | `entries[${number}]`
266 | `entries[${number}].ages`
267 | `entries[${number}].ages[${number}]`
268 | `entries[${number}].names`
269 | `entries[${number}].names[${number}]`
270 | `entries[${number}].users`
271 | `entries[${number}].users[${number}]`
272 | `entries[${number}].users[${number}].name`
273 | `entries[${number}].users[${number}].id`
274 | `entries[${number}].users[${number}].age`
275β
276 expectTypeOf<DeepKeys<TwoDimArrays>>().toEqualTypeOf<ExpectedKeys>()
277 })
278β
279 it('should handle optional arrays', () => {
280 type UsersArray = {
281 nullable: Array<User> | null
282 undefinable: Array<User> | undefined
283 optional?: Array<User>
284 }
285β
286 type ExpectedKeys =
287 | 'nullable'
288 | `nullable[${number}]`
289 | `nullable[${number}].name`
290 | `nullable[${number}].id`
291 | `nullable[${number}].age`
292 | 'undefinable'
293 | `undefinable[${number}]`
294 | `undefinable[${number}].name`
295 | `undefinable[${number}].id`
296 | `undefinable[${number}].age`
297 | 'optional'
298 | `optional[${number}]`
299 | `optional[${number}].name`
300 | `optional[${number}].id`
301 | `optional[${number}].age`
302β
303 expectTypeOf<DeepKeys<UsersArray>>().toEqualTypeOf<ExpectedKeys>()
304 })
305β
306 it('should support top level arrays', () => {
307 type UserArray = Array<User>
308β
309 type ExpectedKeys =
310 `[${number}]` | `[${number}].name` | `[${number}].id` | `[${number}].age`
311β
312 expectTypeOf<DeepKeys<UserArray>>().toEqualTypeOf<ExpectedKeys>()
313 })
314β
315 it('should handle a real use case object', () => {
316 type Stock = {
317 id: string
318 quantity: number
319 isChecked: boolean
320 }
321β
322 type Product = {
323 id: string
324 description?: string
325 price_internet?: number
326 price_dealer_region?: number
327 price_dealer?: number
328 stock: Array<Stock> | null
329 quantity: number
330 isChecked: boolean
331 }
332 type Cart = {
333 id: number
334 product: Product
335 }
336β
337 type Payment_types = Array<{
338 id: string
339 title: string
340 name: string
341 }>
342β
343 type Shipping_methods = Array<{
344 id: string
345 title: string
346 name: string
347 }>
348β
349 type Avatar = {
350 url?: string
351 } & {
352 id: string
353 storage: string
354 filename_disk: string | null
355 filename_original: string | null
356 filename_download: string | null
357 filename_preview: string | null
358 filename_thumbnail: string | null
359 filename_medium: string | null
360 filename_large: string | null
361 filename_huge: string | null
362 filename_icon: string | null
363 filename_icon_large: string | null
364 focal_point_y: number | null
365 }
366β
367 type UserDto = {
368 id: string
369 first_name: string | null
370 email: string | null
371 avatar: string | Avatar | null
372 // Reference Cart, Payment_types, Shipping_methods
373 cart: Cart | null
374 payment_types: Payment_types | null
375 shipping_methods: Shipping_methods | null
376 }
377β
378 type ExpectedKeys =
379 | 'id'
380 | 'first_name'
381 | 'email'
382 | 'avatar'
383 | 'avatar.url'
384 | 'avatar.id'
385 | 'avatar.storage'
386 | 'avatar.filename_disk'
387 | 'avatar.filename_original'
388 | 'avatar.filename_download'
389 | 'avatar.filename_preview'
390 | 'avatar.filename_thumbnail'
391 | 'avatar.filename_medium'
392 | 'avatar.filename_large'
393 | 'avatar.filename_huge'
394 | 'avatar.filename_icon'
395 | 'avatar.filename_icon_large'
396 | 'avatar.focal_point_y'
397 | 'cart'
398 | 'cart.id'
399 | 'cart.product'
400 | 'cart.product.id'
401 | 'cart.product.description'
402 | 'cart.product.price_internet'
403 | 'cart.product.price_dealer_region'
404 | 'cart.product.price_dealer'
405 | 'cart.product.quantity'
406 | 'cart.product.isChecked'
407 | 'cart.product.stock'
408 | `cart.product.stock[${number}]`
409 | `cart.product.stock[${number}].id`
410 | `cart.product.stock[${number}].quantity`
411 | `cart.product.stock[${number}].isChecked`
412 | 'payment_types'
413 | `payment_types[${number}]`
414 | `payment_types[${number}].id`
415 | `payment_types[${number}].title`
416 | `payment_types[${number}].name`
417 | 'shipping_methods'
418 | `shipping_methods[${number}]`
419 | `shipping_methods[${number}].id`
420 | `shipping_methods[${number}].title`
421 | `shipping_methods[${number}].name`
422β
423 expectTypeOf<DeepKeys<UserDto>>().toEqualTypeOf<ExpectedKeys>()
424 })
425β
426 it('should handle records', () => {
427 type Value = {
428 a: string
429 b: number
430 c: { d: string }
431 }
432 type RecordExample = {
433 records: Record<string, Value>
434 }
435β
436 type ExpectedKeys =
437 | 'records'
438 | `records.${string}`
439 | `records.${string}.a`
440 | `records.${string}.b`
441 | `records.${string}.c`
442 | `records.${string}.c.d`
443β
444 expectTypeOf<DeepKeys<RecordExample>>().toEqualTypeOf<ExpectedKeys>()
445 })
446})
447β
448describe('DeepValue', () => {
449 it('Should support tuples with various internal types', () => {
450 type TestShape = { topUsers: [User, 0, User] }
451β
452 type Expect<TKey extends string> = DeepValue<TestShape, TKey>
453β
454 expectTypeOf<Expect<'topUsers'>>().toEqualTypeOf<TestShape['topUsers']>()
455 expectTypeOf<Expect<'topUsers[0]'>>().toEqualTypeOf<User>()
456 expectTypeOf<Expect<'topUsers[1]'>>().toEqualTypeOf<0>()
457 expectTypeOf<Expect<'topUsers[2]'>>().toEqualTypeOf<User>()
458 })
459β
460 it('Should support arrays', () => {
461 type OneElementType = { users: Array<User> }
462β
463 type Expect<TKey extends string> = DeepValue<OneElementType, TKey>
464β
465 expectTypeOf<Expect<'users'>>().toEqualTypeOf<Array<User>>()
466 expectTypeOf<Expect<'users[1]'>>().toEqualTypeOf<User>()
467 expectTypeOf<Expect<'users[15]'>>().toEqualTypeOf<User>()
468 })
469β
470 it('Should allow keys for unions in arrays', () => {
471 type UnionType = { users: Array<User | number> }
472β
473 type Expect<TKey extends string> = DeepValue<UnionType, TKey>
474β
475 expectTypeOf<Expect<'users'>>().toEqualTypeOf<Array<User | number>>()
476 expectTypeOf<Expect<'users[0]'>>().toEqualTypeOf<User | number>()
477 expectTypeOf<Expect<'users[15]'>>().toEqualTypeOf<User | number>()
478 // number['name'] evaluates to undefined
479 expectTypeOf<Expect<'users[15].name'>>().toEqualTypeOf<string | undefined>()
480 expectTypeOf<Expect<'users[15].age'>>().toEqualTypeOf<number | undefined>()
481 expectTypeOf<Expect<'users[15].id'>>().toEqualTypeOf<string | undefined>()
482 })
483β
484 it('Should allow deep nesting', () => {
485 type NestedSupport = { meta: { mainUser: User } }
486β
487 type Expect<TKey extends string> = DeepValue<NestedSupport, TKey>
488β
489 expectTypeOf<Expect<'meta'>>().toEqualTypeOf<{ mainUser: User }>()
490 expectTypeOf<Expect<'meta.mainUser'>>().toEqualTypeOf<User>()
491 expectTypeOf<Expect<'meta.mainUser.name'>>().toEqualTypeOf<string>()
492 expectTypeOf<Expect<'meta.mainUser.id'>>().toEqualTypeOf<string>()
493 expectTypeOf<Expect<'meta.mainUser.age'>>().toEqualTypeOf<number>()
494 })
495β
496 it('Should allow partial objects', () => {
497 type PartialObject = { meta?: { mainUser?: User } }
498β
499 type Expect<TKey extends string> = DeepValue<PartialObject, TKey>
500β
501 expectTypeOf<Expect<'meta'>>().toEqualTypeOf<
502 { mainUser?: User } | undefined
503 >()
504 expectTypeOf<Expect<'meta.mainUser'>>().toEqualTypeOf<User | undefined>()
505 expectTypeOf<Expect<'meta.mainUser.name'>>().toEqualTypeOf<
506 string | undefined
507 >()
508 expectTypeOf<Expect<'meta.mainUser.id'>>().toEqualTypeOf<
509 string | undefined
510 >()
511 expectTypeOf<Expect<'meta.mainUser.age'>>().toEqualTypeOf<
512 number | undefined
513 >()
514 })
515β
516 it('should handle the `object` type', () => {
517 expectTypeOf<DeepValue<object, 'anything'>>().toEqualTypeOf<unknown>()
518β
519 type NestedObject = { meta: { mainUser: object } }
520β
521 type Expect<TKey extends string> = DeepValue<NestedObject, TKey>
522β
523 expectTypeOf<Expect<'meta'>>().toEqualTypeOf<{ mainUser: object }>()
524 expectTypeOf<Expect<'meta.mainUser'>>().toEqualTypeOf<object>()
525 expectTypeOf<Expect<'meta.mainUser.foo'>>().toEqualTypeOf<unknown>()
526 expectTypeOf<Expect<'meta.mainUser.bar'>>().toEqualTypeOf<unknown>()
527 expectTypeOf<
528 Expect<'meta.mainUser.foo.bar.foobar'>
529 >().toEqualTypeOf<unknown>()
530 })
531β
532 it('should handle `unknown` in types', () => {
533 expectTypeOf<DeepValue<unknown, 'foo'>>().toEqualTypeOf<unknown>()
534 expectTypeOf<DeepValue<unknown, 'foo.bar'>>().toEqualTypeOf<unknown>()
535 expectTypeOf<
536 DeepValue<unknown, 'foo.bar.foobar'>
537 >().toEqualTypeOf<unknown>()
538β
539 type NestedUnknown = { meta: { mainUser: unknown } }
540β
541 type Expect<TKey extends string> = DeepValue<NestedUnknown, TKey>
542β
543 expectTypeOf<Expect<'meta'>>().toEqualTypeOf<{ mainUser: unknown }>()
544 expectTypeOf<Expect<'meta.mainUser'>>().toEqualTypeOf<unknown>()
545 expectTypeOf<Expect<'meta.mainUser.foo'>>().toEqualTypeOf<unknown>()
546 expectTypeOf<Expect<'meta.mainUser.foo.bar'>>().toEqualTypeOf<unknown>()
547 expectTypeOf<
548 Expect<'meta.mainUser.foo.bar.foobar'>
549 >().toEqualTypeOf<unknown>()
550 })
551β
552 it('should handle `any` in types', () => {
553 // Whether this is any or unknown is up to us. Change unit test if desired.
554 expectTypeOf<DeepValue<any, 'foo'>>().toEqualTypeOf<any>()
555 expectTypeOf<DeepValue<any, 'foo.bar'>>().toEqualTypeOf<any>()
556 expectTypeOf<DeepValue<any, 'foo.bar.foobar'>>().toEqualTypeOf<any>()
557β
558 type NestedAny = { meta: { mainUser: any } }
559β
560 type Expect<TKey extends string> = DeepValue<NestedAny, TKey>
561β
562 expectTypeOf<Expect<'meta'>>().toEqualTypeOf<{ mainUser: any }>()
563 expectTypeOf<Expect<'meta.mainUser'>>().toEqualTypeOf<any>()
564 expectTypeOf<Expect<'meta.mainUser.foo'>>().toEqualTypeOf<unknown>()
565 expectTypeOf<Expect<'meta.mainUser.foo.bar'>>().toEqualTypeOf<unknown>()
566 expectTypeOf<
567 Expect<'meta.mainUser.foo.bar.foobar'>
568 >().toEqualTypeOf<unknown>()
569 })
570β
571 it('should handle unions and intersections', () => {
572 type DiscriminatedUnion = { name: string } & (
573 { variant: 'foo' } | { variant: 'bar'; baz: boolean }
574 )
575 type Expect<TKey extends string> = DeepValue<DiscriminatedUnion, TKey>
576β
577 expectTypeOf<Expect<'name'>>().toEqualTypeOf<string>()
578 expectTypeOf<Expect<'variant'>>().toEqualTypeOf<'foo' | 'bar'>()
579 expectTypeOf<Expect<'baz'>>().toEqualTypeOf<boolean | undefined>()
580 })
581β
582 it('should handle nullish states', () => {
583 type Insert = { mainUser: 'name' }
584 type NullishStates = {
585 required: Insert
586 nullable: Insert | null
587 undefinable: Insert | undefined
588 optional?: Insert
589 mixed: Insert | null | undefined
590 onlyNull: null
591 onlyUndefined: undefined
592 optionalNull?: null
593 onlyNil: null | undefined
594 }
595β
596 type Expect<TKey extends string> = DeepValue<NullishStates, TKey>
597β
598 expectTypeOf<Expect<'required'>>().toEqualTypeOf<Insert>()
599 expectTypeOf<Expect<'required.mainUser'>>().toEqualTypeOf<'name'>()
600 expectTypeOf<Expect<'nullable'>>().toEqualTypeOf<Insert | null>()
601 expectTypeOf<Expect<'nullable.mainUser'>>().toEqualTypeOf<
602 'name' | undefined
603 >()
604 expectTypeOf<Expect<'undefinable'>>().toEqualTypeOf<Insert | undefined>()
605 expectTypeOf<Expect<'undefinable.mainUser'>>().toEqualTypeOf<
606 'name' | undefined
607 >()
608 expectTypeOf<Expect<'optional'>>().toEqualTypeOf<Insert | undefined>()
609 expectTypeOf<Expect<'optional.mainUser'>>().toEqualTypeOf<
610 'name' | undefined
611 >()
612 expectTypeOf<Expect<'mixed'>>().toEqualTypeOf<Insert | undefined | null>()
613 expectTypeOf<Expect<'mixed.mainUser'>>().toEqualTypeOf<'name' | undefined>()
614 expectTypeOf<Expect<'onlyNull'>>().toEqualTypeOf<null>()
615 expectTypeOf<Expect<'onlyUndefined'>>().toEqualTypeOf<undefined>()
616 expectTypeOf<Expect<'optionalNull'>>().toEqualTypeOf<undefined | null>()
617 expectTypeOf<Expect<'onlyNil'>>().toEqualTypeOf<undefined | null>()
618 })
619β
620 it('should handle nullish states with nested objects', () => {
621 type Roles = { mainUser: { name: 'name' } }
622 type NestedNullishState = {
623 mixed?: Roles | null | undefined
624 }
625β
626 type Expect<TKey extends string> = DeepValue<NestedNullishState, TKey>
627β
628 expectTypeOf<Expect<'mixed'>>().toEqualTypeOf<Roles | null | undefined>()
629 expectTypeOf<Expect<'mixed.mainUser'>>().toEqualTypeOf<
630 { name: 'name' } | undefined
631 >()
632 expectTypeOf<Expect<'mixed.mainUser.name'>>().toEqualTypeOf<
633 'name' | undefined
634 >()
635 })
636β
637 it('should handle unions with nested cases', () => {
638 type TheUnion =
639 | { a: User }
640 | { a: string }
641 | { b: string }
642 | { c: { user: User } | { user: number } }
643 type NestedObjectUnionCase = {
644 normal: TheUnion
645 }
646β
647 type Expect<TKey extends string> = DeepValue<NestedObjectUnionCase, TKey>
648β
649 expectTypeOf<Expect<'normal'>>().toEqualTypeOf<TheUnion>()
650 expectTypeOf<Expect<'normal.a'>>().toEqualTypeOf<
651 User | string | undefined
652 >()
653 expectTypeOf<Expect<'normal.a.id'>>().toEqualTypeOf<string | undefined>()
654 expectTypeOf<Expect<'normal.a.name'>>().toEqualTypeOf<string | undefined>()
655 expectTypeOf<Expect<'normal.a.age'>>().toEqualTypeOf<number | undefined>()
656 expectTypeOf<Expect<'normal.b'>>().toEqualTypeOf<string | undefined>()
657 expectTypeOf<Expect<'normal.c'>>().toEqualTypeOf<
658 { user: User } | { user: number } | undefined
659 >()
660 expectTypeOf<Expect<'normal.c.user'>>().toEqualTypeOf<
661 User | number | undefined
662 >()
663 expectTypeOf<Expect<'normal.c.user.name'>>().toEqualTypeOf<
664 string | undefined
665 >()
666 expectTypeOf<Expect<'normal.c.user.id'>>().toEqualTypeOf<
667 string | undefined
668 >()
669 expectTypeOf<Expect<'normal.c.user.age'>>().toEqualTypeOf<
670 number | undefined
671 >()
672 })
673β
674 it('should handle nullish within nested unions', () => {
675 type TheUnion =
676 | { a?: number; b?: { c: boolean } | null }
677 | { b?: { c: string; e: number } }
678β
679 type NullishNested = {
680 nullable: TheUnion
681 }
682β
683 type Expect<TKey extends string> = DeepValue<NullishNested, TKey>
684β
685 expectTypeOf<Expect<'nullable'>>().toEqualTypeOf<TheUnion>()
686 expectTypeOf<Expect<'nullable.a'>>().toEqualTypeOf<number | undefined>()
687 expectTypeOf<Expect<'nullable.b'>>().toEqualTypeOf<
688 { c: boolean } | { c: string; e: number } | null | undefined
689 >()
690 expectTypeOf<Expect<'nullable.b.c'>>().toEqualTypeOf<
691 boolean | string | undefined
692 >()
693 expectTypeOf<Expect<'nullable.b.e'>>().toEqualTypeOf<number | undefined>()
694 })
695β
696 it('should handle one dimensional arrays', () => {
697 type OneDimArrays = {
698 ages: Array<number>
699 names: Array<string>
700 users: Array<User>
701 }
702β
703 type Expect<TKey extends string> = DeepValue<OneDimArrays, TKey>
704β
705 expectTypeOf<Expect<'ages'>>().toEqualTypeOf<Array<number>>()
706 expectTypeOf<Expect<`ages[${number}]`>>().toEqualTypeOf<number>()
707 expectTypeOf<Expect<'names'>>().toEqualTypeOf<Array<string>>()
708 expectTypeOf<Expect<`names[${number}]`>>().toEqualTypeOf<string>()
709 expectTypeOf<Expect<'users'>>().toEqualTypeOf<Array<User>>()
710 expectTypeOf<Expect<`users[${number}]`>>().toEqualTypeOf<User>()
711 expectTypeOf<Expect<`users[${number}].name`>>().toEqualTypeOf<string>()
712 expectTypeOf<Expect<`users[${number}].id`>>().toEqualTypeOf<string>()
713 expectTypeOf<Expect<`users[${number}].age`>>().toEqualTypeOf<number>()
714 })
715β
716 it('should handle two dimensional arrays', () => {
717 type TwoDimArrays = {
718 ages: Array<Array<number>>
719 names: Array<Array<string>>
720 users: Array<Array<User>>
721 }
722β
723 type Expect<TKey extends string> = DeepValue<TwoDimArrays, TKey>
724β
725 expectTypeOf<Expect<'ages'>>().toEqualTypeOf<Array<Array<number>>>()
726 expectTypeOf<Expect<`ages[${number}]`>>().toEqualTypeOf<Array<number>>()
727 expectTypeOf<Expect<`ages[${number}][${number}]`>>().toEqualTypeOf<number>()
728 expectTypeOf<Expect<'names'>>().toEqualTypeOf<Array<Array<string>>>()
729 expectTypeOf<Expect<`names[${number}]`>>().toEqualTypeOf<Array<string>>()
730 expectTypeOf<
731 Expect<`names[${number}][${number}]`>
732 >().toEqualTypeOf<string>()
733 expectTypeOf<Expect<'users'>>().toEqualTypeOf<Array<Array<User>>>()
734 expectTypeOf<Expect<`users[${number}]`>>().toEqualTypeOf<Array<User>>()
735 expectTypeOf<Expect<`users[${number}][${number}]`>>().toEqualTypeOf<User>()
736 expectTypeOf<
737 Expect<`users[${number}][${number}].name`>
738 >().toEqualTypeOf<string>()
739 expectTypeOf<
740 Expect<`users[${number}][${number}].id`>
741 >().toEqualTypeOf<string>()
742 expectTypeOf<
743 Expect<`users[${number}][${number}].age`>
744 >().toEqualTypeOf<number>()
745 })
746β
747 it('should handle two arrays with object', () => {
748 type TwoDimArrays = {
749 entries: Array<{
750 ages: Array<number>
751 names: Array<string>
752 users: Array<User>
753 }>
754 }
755β
756 type Expect<TKey extends string> = DeepValue<TwoDimArrays, TKey>
757β
758 expectTypeOf<Expect<'entries'>>().toEqualTypeOf<
759 Array<{ ages: Array<number>; names: Array<string>; users: Array<User> }>
760 >()
761 expectTypeOf<Expect<`entries[${number}]`>>().toEqualTypeOf<{
762 ages: Array<number>
763 names: Array<string>
764 users: Array<User>
765 }>()
766 expectTypeOf<Expect<`entries[${number}].ages`>>().toEqualTypeOf<
767 Array<number>
768 >()
769 expectTypeOf<
770 Expect<`entries[${number}].ages[${number}]`>
771 >().toEqualTypeOf<number>()
772 expectTypeOf<Expect<`entries[${number}].names`>>().toEqualTypeOf<
773 Array<string>
774 >()
775 expectTypeOf<
776 Expect<`entries[${number}].names[${number}]`>
777 >().toEqualTypeOf<string>()
778 expectTypeOf<Expect<`entries[${number}].users`>>().toEqualTypeOf<
779 Array<User>
780 >()
781 expectTypeOf<
782 Expect<`entries[${number}].users[${number}]`>
783 >().toEqualTypeOf<User>()
784 expectTypeOf<
785 Expect<`entries[${number}].users[${number}].name`>
786 >().toEqualTypeOf<string>()
787 expectTypeOf<
788 Expect<`entries[${number}].users[${number}].id`>
789 >().toEqualTypeOf<string>()
790 expectTypeOf<
791 Expect<`entries[${number}].users[${number}].age`>
792 >().toEqualTypeOf<number>()
793 })
794β
795 it('should handle optional arrays', () => {
796 type UsersArray = {
797 nullable: Array<User> | null
798 undefinable: Array<User> | undefined
799 optional?: Array<User>
800 }
801β
802 type Expect<TKey extends string> = DeepValue<UsersArray, TKey>
803β
804 expectTypeOf<Expect<'nullable'>>().toEqualTypeOf<Array<User> | null>()
805 expectTypeOf<Expect<`nullable[${number}]`>>().toEqualTypeOf<
806 User | undefined
807 >()
808 expectTypeOf<Expect<`nullable[${number}].name`>>().toEqualTypeOf<
809 string | undefined
810 >()
811 expectTypeOf<Expect<`nullable[${number}].id`>>().toEqualTypeOf<
812 string | undefined
813 >()
814 expectTypeOf<Expect<`nullable[${number}].age`>>().toEqualTypeOf<
815 number | undefined
816 >()
817β
818 expectTypeOf<Expect<'undefinable'>>().toEqualTypeOf<
819 Array<User> | undefined
820 >()
821 expectTypeOf<Expect<`undefinable[${number}]`>>().toEqualTypeOf<
822 User | undefined
823 >()
824 expectTypeOf<Expect<`undefinable[${number}].name`>>().toEqualTypeOf<
825 string | undefined
826 >()
827 expectTypeOf<Expect<`undefinable[${number}].id`>>().toEqualTypeOf<
828 string | undefined
829 >()
830 expectTypeOf<Expect<`undefinable[${number}].age`>>().toEqualTypeOf<
831 number | undefined
832 >()
833β
834 expectTypeOf<Expect<'optional'>>().toEqualTypeOf<Array<User> | undefined>()
835 expectTypeOf<Expect<`optional[${number}]`>>().toEqualTypeOf<
836 User | undefined
837 >()
838 expectTypeOf<Expect<`optional[${number}].name`>>().toEqualTypeOf<
839 string | undefined
840 >()
841 expectTypeOf<Expect<`optional[${number}].id`>>().toEqualTypeOf<
842 string | undefined
843 >()
844 expectTypeOf<Expect<`optional[${number}].age`>>().toEqualTypeOf<
845 number | undefined
846 >()
847 })
848β
849 it('should support top level arrays', () => {
850 type UserArray = Array<User>
851β
852 type Expect<TKey extends string> = DeepValue<UserArray, TKey>
853β
854 expectTypeOf<Expect<'[0]'>>().toEqualTypeOf<User>()
855 expectTypeOf<Expect<'[15]'>>().toEqualTypeOf<User>()
856 expectTypeOf<Expect<`[${number}]`>>().toEqualTypeOf<User>()
857 expectTypeOf<Expect<`[${number}].name`>>().toEqualTypeOf<string>()
858 expectTypeOf<Expect<`[${number}].id`>>().toEqualTypeOf<string>()
859 expectTypeOf<Expect<`[${number}].age`>>().toEqualTypeOf<number>()
860 })
861β
862 it('should handle a real use case object', () => {
863 type Stock = {
864 id: string
865 quantity: number
866 isChecked: boolean
867 }
868β
869 type Product = {
870 id: string
871 description?: string
872 price_internet?: number
873 price_dealer_region?: number
874 price_dealer?: number
875 stock: Array<Stock> | null
876 quantity: number
877 isChecked: boolean
878 }
879 type Cart = {
880 id: number
881 product: Product
882 }
883β
884 type Payment_type = {
885 id: string
886 title: string
887 name: string
888 }
889β
890 type Shipping_method = {
891 id: string
892 title: string
893 name: string
894 }
895β
896 type Avatar = {
897 url?: string
898 } & {
899 id: string
900 storage: string
901 filename_disk: string | null
902 filename_original: string | null
903 filename_download: string | null
904 filename_preview: string | null
905 filename_thumbnail: string | null
906 filename_medium: string | null
907 filename_large: string | null
908 filename_huge: string | null
909 filename_icon: string | null
910 filename_icon_large: string | null
911 focal_point_y: number | null
912 }
913β
914 type UserDto = {
915 id: string
916 first_name: string | null
917 email: string | null
918 avatar: string | Avatar | null
919 // Reference Cart, Payment_types, Shipping_methods
920 cart: Cart | null
921 payment_types: Array<Payment_type> | null
922 shipping_methods: Array<Shipping_method> | null
923 }
924β
925 type Expect<TKey extends string> = DeepValue<UserDto, TKey>
926β
927 expectTypeOf<Expect<'id'>>().toEqualTypeOf<string>()
928 expectTypeOf<Expect<'first_name'>>().toEqualTypeOf<string | null>()
929 expectTypeOf<Expect<'email'>>().toEqualTypeOf<string | null>()
930 expectTypeOf<Expect<'avatar'>>().toEqualTypeOf<string | Avatar | null>()
931 expectTypeOf<Expect<'avatar.url'>>().toEqualTypeOf<string | undefined>()
932 expectTypeOf<Expect<'avatar.id'>>().toEqualTypeOf<string | undefined>()
933 expectTypeOf<Expect<'avatar.storage'>>().toEqualTypeOf<string | undefined>()
934 expectTypeOf<Expect<'avatar.filename_disk'>>().toEqualTypeOf<
935 string | null | undefined
936 >()
937 expectTypeOf<Expect<'avatar.filename_original'>>().toEqualTypeOf<
938 string | null | undefined
939 >()
940 expectTypeOf<Expect<'avatar.filename_download'>>().toEqualTypeOf<
941 string | null | undefined
942 >()
943 expectTypeOf<Expect<'avatar.filename_preview'>>().toEqualTypeOf<
944 string | null | undefined
945 >()
946 expectTypeOf<Expect<'avatar.filename_thumbnail'>>().toEqualTypeOf<
947 string | null | undefined
948 >()
949 expectTypeOf<Expect<'avatar.filename_medium'>>().toEqualTypeOf<
950 string | null | undefined
951 >()
952 expectTypeOf<Expect<'avatar.filename_large'>>().toEqualTypeOf<
953 string | null | undefined
954 >()
955 expectTypeOf<Expect<'avatar.filename_huge'>>().toEqualTypeOf<
956 string | null | undefined
957 >()
958 expectTypeOf<Expect<'avatar.filename_icon'>>().toEqualTypeOf<
959 string | null | undefined
960 >()
961 expectTypeOf<Expect<'avatar.filename_icon_large'>>().toEqualTypeOf<
962 string | null | undefined
963 >()
964 expectTypeOf<Expect<'avatar.focal_point_y'>>().toEqualTypeOf<
965 number | null | undefined
966 >()
967 expectTypeOf<Expect<'cart'>>().toEqualTypeOf<Cart | null>()
968 expectTypeOf<Expect<'cart.id'>>().toEqualTypeOf<number | undefined>()
969 expectTypeOf<Expect<'cart.product'>>().toEqualTypeOf<Product | undefined>()
970 expectTypeOf<Expect<'cart.product.id'>>().toEqualTypeOf<
971 string | undefined
972 >()
973 expectTypeOf<Expect<'cart.product.description'>>().toEqualTypeOf<
974 string | undefined
975 >()
976 expectTypeOf<Expect<'cart.product.price_internet'>>().toEqualTypeOf<
977 number | undefined
978 >()
979 expectTypeOf<Expect<'cart.product.price_dealer_region'>>().toEqualTypeOf<
980 number | undefined
981 >()
982 expectTypeOf<Expect<'cart.product.price_dealer'>>().toEqualTypeOf<
983 number | undefined
984 >()
985 expectTypeOf<Expect<'cart.product.quantity'>>().toEqualTypeOf<
986 number | undefined
987 >()
988 expectTypeOf<Expect<'cart.product.isChecked'>>().toEqualTypeOf<
989 boolean | undefined
990 >()
991 expectTypeOf<Expect<'cart.product.stock'>>().toEqualTypeOf<
992 Array<Stock> | null | undefined
993 >()
994 expectTypeOf<Expect<`cart.product.stock[${number}]`>>().toEqualTypeOf<
995 Stock | undefined
996 >()
997 expectTypeOf<Expect<`cart.product.stock[${number}].id`>>().toEqualTypeOf<
998 string | undefined
999 >()
1000 expectTypeOf<
1001 Expect<`cart.product.stock[${number}].quantity`>
1002 >().toEqualTypeOf<number | undefined>()
1003 expectTypeOf<
1004 Expect<`cart.product.stock[${number}].isChecked`>
1005 >().toEqualTypeOf<boolean | undefined>()
1006 expectTypeOf<
1007 Expect<'payment_types'>
1008 >().toEqualTypeOf<Array<Payment_type> | null>()
1009 expectTypeOf<Expect<`payment_types[${number}]`>>().toEqualTypeOf<
1010 Payment_type | undefined
1011 >()
1012 expectTypeOf<Expect<`payment_types[${number}].id`>>().toEqualTypeOf<
1013 string | undefined
1014 >()
1015 expectTypeOf<Expect<`payment_types[${number}].title`>>().toEqualTypeOf<
1016 string | undefined
1017 >()
1018 expectTypeOf<Expect<`payment_types[${number}].name`>>().toEqualTypeOf<
1019 string | undefined
1020 >()
1021 expectTypeOf<
1022 Expect<'shipping_methods'>
1023 >().toEqualTypeOf<Array<Shipping_method> | null>()
1024 expectTypeOf<Expect<`shipping_methods[${number}]`>>().toEqualTypeOf<
1025 Shipping_method | undefined
1026 >()
1027 expectTypeOf<Expect<`shipping_methods[${number}].id`>>().toEqualTypeOf<
1028 string | undefined
1029 >()
1030 expectTypeOf<Expect<`shipping_methods[${number}].title`>>().toEqualTypeOf<
1031 string | undefined
1032 >()
1033 expectTypeOf<Expect<`shipping_methods[${number}].name`>>().toEqualTypeOf<
1034 string | undefined
1035 >()
1036 })
1037β
1038 it('should handle objects with any', () => {
1039 type ObjectWithAny = {
1040 a: any
1041 b: number
1042 obj: {
1043 c: any
1044 d: number
1045 }
1046 }
1047β
1048 type ExpectedKeys =
1049 'a' | `a.${string}` | 'b' | 'obj' | 'obj.c' | `obj.c.${string}` | 'obj.d'
1050β
1051 expectTypeOf<DeepKeys<ObjectWithAny>>().toEqualTypeOf<ExpectedKeys>()
1052β
1053 type Expect<TKey extends string> = DeepValue<ObjectWithAny, TKey>
1054β
1055 expectTypeOf<Expect<'a'>>().toEqualTypeOf<any>()
1056 expectTypeOf<Expect<'a.anything'>>().toEqualTypeOf<unknown>()
1057 expectTypeOf<Expect<`a.${string}`>>().toEqualTypeOf<unknown>()
1058 expectTypeOf<Expect<'b'>>().toEqualTypeOf<number>()
1059 expectTypeOf<Expect<'obj'>>().toEqualTypeOf<{ c: any; d: number }>()
1060 expectTypeOf<Expect<'obj.c'>>().toEqualTypeOf<any>()
1061 expectTypeOf<Expect<'obj.c.anything'>>().toEqualTypeOf<unknown>()
1062 expectTypeOf<Expect<`obj.c.${string}`>>().toEqualTypeOf<unknown>()
1063 expectTypeOf<Expect<'obj.d'>>().toEqualTypeOf<number>()
1064 })
1065β
1066 it('should handle records', () => {
1067 type Value = {
1068 a: string
1069 b: number
1070 c: { d: string }
1071 }
1072 type RecordExample = {
1073 records: Record<string, Value>
1074 }
1075β
1076 type Expect<TKey extends string> = DeepValue<RecordExample, TKey>
1077β
1078 expectTypeOf<Expect<'records'>>().toEqualTypeOf<Record<string, Value>>()
1079 expectTypeOf<Expect<'records.foo'>>().toEqualTypeOf<Value>()
1080 expectTypeOf<Expect<'records.foo.a'>>().toEqualTypeOf<string>()
1081 expectTypeOf<Expect<'records.foo.b'>>().toEqualTypeOf<number>()
1082 expectTypeOf<Expect<'records.foo.c'>>().toEqualTypeOf<{ d: string }>()
1083 expectTypeOf<Expect<'records.foo.c.d'>>().toEqualTypeOf<string>()
1084 expectTypeOf<Expect<`records.${string}`>>().toEqualTypeOf<Value>()
1085 expectTypeOf<Expect<`records.${string}.a`>>().toEqualTypeOf<string>()
1086 expectTypeOf<Expect<`records.${string}.b`>>().toEqualTypeOf<number>()
1087 expectTypeOf<Expect<`records.${string}.c`>>().toEqualTypeOf<{ d: string }>()
1088 expectTypeOf<Expect<`records.${string}.c.d`>>().toEqualTypeOf<string>()
1089 })
1090})
1091β
1092describe('DeepKeysWhereValueIncludes', () => {
1093 it('should allow unions to pass', () => {
1094 type Example = {
1095 foo: Array<{ bar: string }> | null | undefined
1096 bar: Array<{ bar: string }> | string | number
1097 foobar: Array<{ bar: string } | { different: string }>
1098 }
1099β
1100 type ExpectedKeys =
1101 | 'foo'
1102 | `foo[${number}]`
1103 | `foo[${number}].bar`
1104 | 'bar'
1105 | `bar[${number}]`
1106 | `bar[${number}].bar`
1107 | 'foobar'
1108 | `foobar[${number}]`
1109 | `foobar[${number}].bar`
1110 | `foobar[${number}].different`
1111β
1112 expectTypeOf<DeepKeys<Example>>().toEqualTypeOf<ExpectedKeys>()
1113β
1114 expectTypeOf<
1115 DeepKeysWhereValueIncludes<Example, Array<any>>
1116 >().toEqualTypeOf<'foo' | 'bar' | 'foobar'>()
1117 })
1118β
1119 it('should match readonly arrays', () => {
1120 type Example = {
1121 mutable: Array<string>
1122 readonly: ReadonlyArray<number>
1123 tuple: readonly [boolean]
1124 value: string
1125 }
1126β
1127 expectTypeOf<
1128 DeepKeysWhereValueIncludes<Example, ReadonlyArray<any>>
1129 >().toEqualTypeOf<'mutable' | 'readonly' | 'tuple'>()
1130 })
1131})
1132β
1133describe('TryGetArrayElementType', () => {
1134 it('should infer elements from mutable and readonly arrays', () => {
1135 expectTypeOf<
1136 TryGetArrayElementType<Array<string>>
1137 >().toEqualTypeOf<string>()
1138 expectTypeOf<
1139 TryGetArrayElementType<ReadonlyArray<number>>
1140 >().toEqualTypeOf<number>()
1141 expectTypeOf<
1142 TryGetArrayElementType<readonly [boolean]>
1143 >().toEqualTypeOf<boolean>()
1144 })
1145})