【发布时间】:2020-11-19 03:12:25
【问题描述】:
将utility-types 中的DeepPartial 泛型类型应用于Typescript 类型时,我注意到所有属性的类型都自动扩展为包括undefined。 IE。以前类型为 T 的属性现在变为 T | undefined。
例如以下代码未通过类型检查:
import { DeepPartial } from 'utility-types';
type A = { a1: number, a2: number};
type B = DeepPartial<A>;
const b: B = { a1: null };
Typescript Playground 是 here。
我知道这也是普通的 Partial 所做的。但是有没有更深层次的原因为什么选择| undefined(在这两种情况下)而不是相反,例如to:| null 或 | null | undefined,有没有办法可以自定义 DeepPartial 泛型类型,以便使用 | null 生成属性类型?
【问题讨论】:
标签: typescript typescript-generics typechecking type-constraints