说我有这样的类型;
interface State {
one: string,
two: {
three: {
four: string
},
five: string
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样制作州部分 Partial<State>
但是我如何才能使嵌套属性成为局部,例如,如果我想使其two
也部分.
我该怎么做?
CRi*_*ice 45
您可以非常轻松地定义自己的RecursivePartial
类型,这将使所有属性(包括嵌套的属性)可选:
type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
Run Code Online (Sandbox Code Playgroud)
如果您只想要将某些属性设置为部分属性,则可以将其与交叉点一起使用Pick
:
type PartialExcept<T, K extends keyof T> = RecursivePartial<T> & Pick<T, K>;
Run Code Online (Sandbox Code Playgroud)
这将使一切都是可选的,除了K
参数中指定的键.
Sha*_*ard 13
这是可能的,您可以创建一个"深度"部分类型,如下所示:
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
Run Code Online (Sandbox Code Playgroud)
可以按照以下方式使用
const state: DeepPartial<State> = {
two: {
three: {
four: '4'
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于 TypeScript 2.8 或更高版本,以下类型应该修复有关 Array 属性的问题:
type NestedPartial<T> = {
[K in keyof T]?: T[K] extends Array<infer R> ? Array<NestedPartial<R>> : NestedPartial<T[K]>
};
Run Code Online (Sandbox Code Playgroud)
请看下面的例子。
interface Foo {
NumProp: number;
SubItem: Foo;
SubItemArray: Foo[];
}
Run Code Online (Sandbox Code Playgroud)
条件类型的有效结果
结果无效
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html