英文:
Nested Pick definition
问题
在函数的定义中,我只关心选取address
的lineOne
部分,但据我了解,Pick
只允许我深入一层,而不允许我进一步指定我的意思。
是否有一种方法可以在 TypeScript 中实现这个目标?
英文:
If I had an interface with an embedded object such as:
interface IStudent {
name: string;
address: {
lineOne: string;
lineTwo: string;
}
}
const getAddressLineOne = (student: Pick<IStudent, 'address'>) => student.address.lineOne;
For the definition of the function, I'd really only care pick the lineOne
part of address
, but as far as I understand, Pick
only allows me to go one level deep, rather than specifying further what I mean.
Is there a way to do this with typescript?
答案1
得分: 3
你可以创建自己的NestedPick
类型:
type NestedPick<T, K extends string[]> = T extends object ? {
[P in Extract<keyof T, K[0]>]: NestedPick<T[P], Tail<K>>
} : T
// 获取元组的尾部
type Tail<T extends any[]> = ((...args: T) => any) extends (head: any, ...tail: infer I) => any
? I : never
尚未测试边缘情况,但这可能是一个很好的起点。然后,getAddressLineOne
看起来像这样:
const getAddressLineOne = (student: NestedPick<IStudent, ['address', 'lineOne']>) =>
student.address.lineOne;
// 测试
declare const student: IStudent
const res = getAddressLineOne(student) // string
或者,你可以缩小getAddressLineOne
函数的参数:
const getAddressLineOne2 = (address: Pick<IStudent["address"], "lineOne">) => address.lineOne;
英文:
You can create your own NestedPick
type:
type NestedPick<T, K extends string[]> = T extends object ? {
[P in Extract<keyof T, K[0]>]: NestedPick<T[P], Tail<K>>
} : T
// get tail of tuple
type Tail<T extends any[]> = ((...args: T) => any) extends (head: any, ...tail: infer I) => any
? I : never
Haven't tested edge cases, but this might be a good starting point. Then getAddressLineOne
looks like:
const getAddressLineOne = (student: NestedPick<IStudent, ['address', 'lineOne']>) =>
student.address.lineOne;
// Test
declare const student: IStudent
const res = getAddressLineOne(student) // string
Alernatively, you could narrow getAddressLineOne
function parameter:
const getAddressLineOne2 = (address: Pick<IStudent["address"], "lineOne">) => address.lineOne;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论