-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00553-hard-deep-object-to-unique.ts
47 lines (38 loc) · 1.32 KB
/
00553-hard-deep-object-to-unique.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
// ============= Test Cases =============
import type { Equal, IsFalse, IsTrue } from './test-utils'
type Quz = { quz: 4 }
type Foo = { foo: 2; baz: Quz; bar: Quz }
type Bar = { foo: 2; baz: Quz; bar: Quz & { quzz?: 0 } }
type UniqQuz = DeepObjectToUniq<Quz>
type UniqFoo = DeepObjectToUniq<Foo>
type UniqBar = DeepObjectToUniq<Bar>
declare let foo: Foo
declare let uniqFoo: UniqFoo
uniqFoo = foo
foo = uniqFoo
type cases = [
IsFalse<Equal<UniqQuz, Quz>>,
IsFalse<Equal<UniqFoo, Foo>>,
IsTrue<Equal<UniqFoo['foo'], Foo['foo']>>,
IsTrue<Equal<UniqFoo['bar']['quz'], Foo['bar']['quz']>>,
IsFalse<Equal<UniqQuz, UniqFoo['baz']>>,
IsFalse<Equal<UniqFoo['bar'], UniqFoo['baz']>>,
IsFalse<Equal<UniqBar['baz'], UniqFoo['baz']>>,
IsTrue<Equal<keyof UniqBar['baz'], keyof UniqFoo['baz']>>,
IsTrue<Equal<keyof Foo, keyof UniqFoo & string>>
]
// ============= Your Code Here =============
// type DeepObjectToUniq<T extends object, U extends unknown[] = [T]> = {
// [P in keyof T]: T[P] extends object ? DeepObjectToUniq<T[P], [T, P]> : T[P]
// } & {
// [key: symbol]: U
// }
declare const sym: unique symbol
type DeepObjectToUniq<T extends object> = {
[P in keyof T]: T[P] extends object
? DeepObjectToUniq<T[P]> & { readonly [sym]?: [T, P] }
: T[P]
} & {
readonly [sym]?: unknown
}
type Test = DeepObjectToUniq<Bar>