export type PartialOption<T,K extends string[]> = {[P in K]?: T[P]}
type Student = PartialOption<People, ['age', 'type']>
// So Student will like this
{
age?: number,
type?: string,
name: string
...
}
我的打字稿版本:“打字稿”:“^3.1.2”,我如何定义 PartialOption?
您只需要使用联合类型而不是元组类型。
export type PartialOption<T,K extends keyof T> = {[P in K]?: T[P]}
type Student = PartialOption<People, 'age' | 'type'>
事实上,这实际上等同于使用的 bultinPartial
类型Pick<T, K>
。
如果您只想使某些属性可选,则可以使用Partial
标记可选属性并Pick
提取其余属性而不更改其可选性
export type PartialOption<T,K extends keyof T> = Partial<Pick<T, K>> & Pick<T, Exclude<keyof T, K>>
type Student = PartialOption<People, 'age' | 'type'>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句