[Typescript] 125. Extreme - Object Key Paths
Get all possible paths that could be called by _.get (a lodash function) to get the value of an object
type T1 = ObjectKeyPaths<{ name: string; age: number }>; // expected to be 'name' | 'age'
type T2 = ObjectKeyPaths<{
refCount: number;
person: { name: string; age: number };
}>; // expected to be 'refCount' | 'person' | 'person.name' | 'person.age'
type T3 = ObjectKeyPaths<{ books: [{ name: string; price: number }] }>; // expected to be the superset of 'books' | 'books.0' | 'books[0]' | 'books.[0]' | 'books.0.name' | 'books.0.price' | 'books.length' | 'books.find'
/* _____________ Your Code Here _____________ */
type GenNode<K extends string | number, IsRoot extends boolean> = IsRoot extends true? `${K}`: `.${K}` | (K extends number? `[${K}]` | `.[${K}]`:never)
type ObjectKeyPaths<
T extends object,
IsRoot extends boolean = true,
K extends keyof T = keyof T
> =
K extends string | number ?
GenNode<K,IsRoot> | (T[K] extends object? `${GenNode<K,IsRoot>}${ObjectKeyPaths<T[K],false>}`:never)
:never;
/* _____________ Test Cases _____________ */
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>>,
Expect<Equal<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.notExist'>, false>>,
Expect<Equal<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.name.'>, false>>,
Expect<Equal<ExpectExtends<ObjectKeyPaths<typeof ref>, '.person.name'>, false>>,
Expect<Equal<ExpectExtends<ObjectKeyPaths<typeof ref>, 'person.pets.[0]type'>, false>>,
]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-12-03 [AWS] Using EC2 Roles and Instance Profiles in AWS
2019-12-03 [Algorithm] 122. Best Time to Buy and Sell Stock II
2019-12-03 [ARIA] Create an Accessible Tooltip on a Text Input
2019-12-03 [Algorithm] 121. Best Time to Buy and Sell Stock
2019-12-03 [Go] Filter an Array in Go
2019-12-03 [GO] Remove Elements from a slice in Go
2019-12-03 [Go] Iterate over an Array or Slice in Go