【发布时间】:2020-02-07 14:50:29
【问题描述】:
我有以下类型:
interface A {
p1: string
p2?: string
}
我想生成一个子类型B,并将可选属性转换为可为空的属性。相当于:
interface B {
p1: string
p2: string | null
}
我尝试过这样的事情:
type VuexPick<T, K extends keyof T> = {
[P in K]-?: T[P] extends undefined ? null : T[P];
};
type B = VuexPick<A, "p1" | "p2">;
但它不起作用。有什么想法吗?
【问题讨论】:
标签: typescript null undefined conditional-types mapped-types