有没有办法在 TypeScript 中指定一个部分类型,它也使所有子对象也成为部分对象?例如:
interface Foobar {
foo: number;
bar: {
baz: boolean;
qux: string;
};
}
const foobar: Partial<Foobar> = {
foo: 1,
bar: { baz: true }
};
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
TS2741: Property 'qux' is missing in type '{ baz: true; }' but required in type '{ baz: boolean; qux: string; }'.
有没有办法使子节点也部分化?
Ter*_*rry 79
您可以简单地创建一个新类型,例如,DeepPartial,它基本上引用自身:
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
Run Code Online (Sandbox Code Playgroud)
然后,您可以这样使用它:
const foobar: DeepPartial<Foobar> = {
foo: 1,
bar: { baz: true }
};
Run Code Online (Sandbox Code Playgroud)
请参阅 TypeScript Playground 上的概念验证示例。
gus*_*nke 13
我从这个问题的答案中得到启发,创建了我自己的 PartialDeep 版本。
一路上我偶然发现了内置对象的一些问题;对于我的用例,我不希望Date对象缺少某些方法。它要么存在,要么不存在。
这是我的版本:
// Primitive types (+ Date) are themselves. Or maybe undefined.
type PartialDeep<T> = T extends string | number | bigint | boolean | null | undefined | symbol | Date
? T | undefined
// Arrays, Sets and Maps and their readonly counterparts have their items made
// deeply partial, but their own instances are left untouched
: T extends Array<infer ArrayType>
? Array<PartialDeep<ArrayType>>
: T extends ReadonlyArray<infer ArrayType>
? ReadonlyArray<ArrayType>
: T extends Set<infer SetType>
? Set<PartialDeep<SetType>>
: T extends ReadonlySet<infer SetType>
? ReadonlySet<SetType>
: T extends Map<infer KeyType, infer ValueType>
? Map<PartialDeep<KeyType>, PartialDeep<ValueType>>
: T extends ReadonlyMap<infer KeyType, infer ValueType>
? ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>>
// ...and finally, all other objects.
: {
[K in keyof T]?: PartialDeep<T[K]>;
};
Run Code Online (Sandbox Code Playgroud)
ash*_*sut 10
DeepPartialDeepPartial当其属性的object类型适用DeepPartial于该属性的属性时,基本上会引用自身,依此类推
type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]
}
Run Code Online (Sandbox Code Playgroud)
interface Foobar {
foo: number;
bar: {
foo1: boolean;
bar1: string;
};
}
const foobar: DeepPartial<Foobar> = {
foo: 1,
bar: { foo1: true }
};
Run Code Online (Sandbox Code Playgroud)
我必须使用这个版本来防止数组具有未定义的元素:
type DeepPartial<T> = T extends any[]? T : T extends Record<string, any> ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : T;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15241 次 |
| 最近记录: |