The get
function in lodash is a quite convenient helper for accessing nested values in JavaScript. However, when we come to TypeScript, using functions like this will make you lose the type information. With TS 4.1's upcoming Template Literal Types feature, properly typing get
becomes possible. Can you implement it?
For example,
type Data = {
foo: {
bar: {
value: 'foobar',
count: 6,
},
included: true,
},
hello: 'world'
}
type A = Get<Data, 'hello'> // 'world'
type B = Get<Data, 'foo.bar.count'> // 6
type C = Get<Data, 'foo.bar'> // { value: 'foobar', count: 6 }
Accessing arrays is not required in this challenge.
/* _____________ Your Code Here _____________ */
type Get<T extends Record<PropertyKey, any>, K extends string> = K extends keyof T
? T[K]
: K extends `${infer P}.${infer U}`
? Get<T[P], U>
: never;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Get<Data, 'hello'>, 'world'>>,
Expect<Equal<Get<Data, 'foo.bar.count'>, 6>>,
Expect<Equal<Get<Data, 'foo.bar'>, { value: 'foobar'; count: 6 }>>,
Expect<Equal<Get<Data, 'foo.baz'>, false>>,
Expect<Equal<Get<Data, 'no.existed'>, never>>,
]
type Data = {
foo: {
bar: {
value: 'foobar'
count: 6
}
included: true
}
'foo.baz': false
hello: 'world'
}
标签:Typescript,_____________,Get,Typed,Expect,foobar,world,type From: https://www.cnblogs.com/Answer1215/p/16891084.html