|
| 1 | +import { describe, expectTypeOf, it } from 'vitest' |
| 2 | + |
| 3 | +// types |
1 | 4 | import type { CollapsiblePaths } from '../src/utils/deep-keys' |
2 | 5 |
|
3 | 6 | type WithDeeplyNestedObject = { |
@@ -65,3 +68,55 @@ type WithGeneric<TData> = { |
65 | 68 | } |
66 | 69 |
|
67 | 70 | type _WithGeneric = CollapsiblePaths<WithGeneric<{ a: { b: string } }>> |
| 71 | + |
| 72 | +describe('deep-keys', () => { |
| 73 | + it('should type deeply nested keys', () => { |
| 74 | + expectTypeOf<_DeeplyNestedObject>().toEqualTypeOf< |
| 75 | + | '' |
| 76 | + | 'a' |
| 77 | + | 'a.b' |
| 78 | + | 'a.b.c' |
| 79 | + | 'a.b.c.d' |
| 80 | + | 'a.b.c.d.e' |
| 81 | + | 'a.b.c.d.e.f' |
| 82 | + | 'a.b.c.d.e.f.g' |
| 83 | + | 'a.b.c.d.e.f.g.h' |
| 84 | + | 'a.b.c.d.e.f.g.h.i' |
| 85 | + >() |
| 86 | + }) |
| 87 | + |
| 88 | + it('should handle any', () => { |
| 89 | + expectTypeOf<_Any>().toEqualTypeOf<'' | 'errors'>() |
| 90 | + }) |
| 91 | + |
| 92 | + it('should handle array recursion', () => { |
| 93 | + expectTypeOf<_ArrayRecursion>().toEqualTypeOf< |
| 94 | + | '' |
| 95 | + | 'arr' |
| 96 | + | `arr[${number}]` |
| 97 | + | `arr[${number}][${number}]` |
| 98 | + | `arr[${number}][${number}][${number}]` |
| 99 | + | `arr[${number}][${number}][${number}][${number}]` |
| 100 | + >() |
| 101 | + }) |
| 102 | + |
| 103 | + it('should handle undefined', () => { |
| 104 | + expectTypeOf<_WithUndefined>().toEqualTypeOf< |
| 105 | + '' | 'status' | 'status.error' |
| 106 | + >() |
| 107 | + }) |
| 108 | + |
| 109 | + it('should handle unknown', () => { |
| 110 | + expectTypeOf<_WithUnknown>().toEqualTypeOf<''>() |
| 111 | + }) |
| 112 | + |
| 113 | + it('should handle realistic state', () => { |
| 114 | + expectTypeOf<_WithRealisticState>().toEqualTypeOf< |
| 115 | + '' | 'errors' | 'errorMap' | `errors[${number}]` | `errorMap.${string}` |
| 116 | + >() |
| 117 | + }) |
| 118 | + |
| 119 | + it('should handle generics', () => { |
| 120 | + expectTypeOf<_WithGeneric>().toEqualTypeOf<'' | 'generic' | 'generic.a'>() |
| 121 | + }) |
| 122 | +}) |
0 commit comments