Ric*_*ler 5 typescript conditional-types typescript2.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"
Run Code Online (Sandbox Code Playgroud)
除了删除对象的属性外,我想这样做。如何实现以下目标:
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; }
Run Code Online (Sandbox Code Playgroud)
好吧,利用之前的Diff类型(顺便说一下,Exclude它与现在属于标准库的类型相同),您可以编写:
type ObjectDiff<T, U> = Pick<T, Diff<keyof T, keyof U>>;
type AWithoutStuff = ObjectDiff<A, Stuff>; // inferred as { one: string; two: number; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |