Typescript 2.8:从一种类型中删除另一种类型的属性
Typescript 2.8: Remove properties in one type from another
在 changelog of 2.8 中,他们有条件类型的示例:
type Diff<T, U> = T extends U ? never : T; // Remove types from T that are assignable to U
type T30 = Diff<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
除了删除对象的属性外,我想这样做。我怎样才能实现以下目标:
type ObjectDiff<T, U> = /* ...pls help... */;
type A = { one: string; two: number; three: Date; };
type Stuff = { three: Date; };
type AWithoutStuff = ObjectDiff<A, Stuff>; // { one: string; two: number; }
好吧,利用之前的 Diff
类型(顺便说一句,它与现在属于标准库的 Exclude
类型相同),您可以这样写:
type ObjectDiff<T, U> = Pick<T, Diff<keyof T, keyof U>>;
type AWithoutStuff = ObjectDiff<A, Stuff>; // inferred as { one: string; two: number; }
在 changelog of 2.8 中,他们有条件类型的示例:
type Diff<T, U> = T extends U ? never : T; // Remove types from T that are assignable to U
type T30 = Diff<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
除了删除对象的属性外,我想这样做。我怎样才能实现以下目标:
type ObjectDiff<T, U> = /* ...pls help... */;
type A = { one: string; two: number; three: Date; };
type Stuff = { three: Date; };
type AWithoutStuff = ObjectDiff<A, Stuff>; // { one: string; two: number; }
好吧,利用之前的 Diff
类型(顺便说一句,它与现在属于标准库的 Exclude
类型相同),您可以这样写:
type ObjectDiff<T, U> = Pick<T, Diff<keyof T, keyof U>>;
type AWithoutStuff = ObjectDiff<A, Stuff>; // inferred as { one: string; two: number; }