Type Challenges Judge

Object Key Paths

提出詳細

type PathNode<K extends PropertyKey, IsRoot> = K extends string | number ? IsRoot extends true ? `${K}` : K extends string ? `.${K}` : `.${K}` | `[${K}]` | `.[${K}]` : ""; type ObjectKeyPaths<T extends object, IsRoot = true, K extends keyof T = keyof T> = T extends unknown[] ? PathNode<number, IsRoot> | (T[number] extends object ? `${PathNode<number, IsRoot>}${ObjectKeyPaths<T[number], false>}` : never) : T extends object ? K extends K ? PathNode<K, IsRoot> | (T[K] extends object ? `${PathNode<K, IsRoot>}${ObjectKeyPaths<T[K], false>}` : never) : never : PathNode<K, IsRoot>;
提出日時2025-01-21 11:28:44
問題Object Key Paths
ユーザーookkoouu
ステータスAccepted
テストケース
import type { Equal, Expect, ExpectExtends } from '@type-challenges/utils' const ref = { count: 1, person: { name: 'cattchen', age: 22, books: ['book1', 'book2'], pets: [ { type: 'cat', }, ], }, } type cases = [ Expect<Equal<ObjectKeyPaths<{ name: string; age: number }>, 'name' | 'age'>>, Expect< Equal< ObjectKeyPaths<{ refCount: number person: { name: string; age: number } }>, 'refCount' | 'person' | 'person.name' | 'person.age' > >, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'count'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.name'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.age'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.books'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.pets'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.books.0'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.books.1'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.books[0]'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.books.[0]'>>, Expect<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.pets.0.type'>>, Expect<Equal<ExpectExtends<ObjectKeyPaths<typeof ref>, 'notExist'>, false>>, ]