我想从类型中排除一个属性。我该怎么做呢?

例如,我有

interface XYZ {
  x: number;
  y: number;
  z: number;
}

我要排除性质z

type XY = { x: number, y: number };

我已经找到了解决方案,声明一些变量和使用扩散运算符推断类型:

interface XYZ {
  x: number;
  y: number;
  z: number;
}

declare var { z, ...xy }: XYZ;

type XY = typeof xy; // { x: number; y: number; }

它是有效的,但我很高兴看到一个更好的解决方案。


对于TypeScript 3.5或以上的版本

在TypeScript 3.5中,省略类型被添加到标准库中。关于如何使用它,请参见下面的示例。

对于TypeScript 3.5以下的版本

在TypeScript 2.8中,Exclude类型被添加到标准库中,这使得省略类型可以简单地写成:

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

对于TypeScript 2.8以下的版本

在2.8以下的版本中不能使用Exclude类型,但是可以为它创建一个替换,以便使用与上面相同的定义类型。但是,这个替换只适用于字符串类型,所以它没有Exclude那么强大。

// Functionally the same as Exclude, but for strings only.
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>

下面是该类型的例子:

interface Test {
    a: string;
    b: number;
    c: boolean;
}

// Omit a single property:
type OmitA = Omit<Test, "a">; // Equivalent to: {b: number, c: boolean}

// Or, to omit multiple properties:
type OmitAB = Omit<Test, "a"|"b">; // Equivalent to: {c: boolean}

在typescript 2.8中,可以使用新的内置Exclude类型。2.8版本说明实际上在“预定义条件类型”一节中提到了这一点:

注意:Exclude类型是Diff类型的正确实现 建议在这里。[…我们没有包括省略类型,因为 它可以简单地写成Pick<T, Exclude<keyof T, K>>。

将此应用到您的示例中,类型XY可以定义为:

type XY = Pick<XYZ, Exclude<keyof XYZ, "z">>

如果您更喜欢使用库,请使用ts-essentials。

import { Omit } from "ts-essentials";

type ComplexObject = {
  simple: number;
  nested: {
    a: string;
    array: [{ bar: number }];
  };
};

type SimplifiedComplexObject = Omit<ComplexObject, "nested">;

// Result:
// {
//  simple: number
// }

// if you want to Omit multiple properties just use union type:
type SimplifiedComplexObject = Omit<ComplexObject, "nested" | "simple">;

// Result:
// { } (empty type)

PS:你会发现很多其他有用的东西;)


打字稿3.5

从Typescript 3.5开始,省略helper将被包括在内

您可以直接使用它,并且在更新时应该删除自己对省略帮助器的定义。


在Typescript 3.5+:

interface TypographyProps {
    variant: string
    fontSize: number
}

type TypographyPropsMinusVariant = Omit<TypographyProps, "variant">

我喜欢这样:

interface XYZ {
  x: number;
  y: number;
  z: number;
}
const a:XYZ = {x:1, y:2, z:3};
const { x, y, ...last } = a;
const { z, ...firstTwo} = a;
console.log(firstTwo, last);

省略……

单一属性

type T1 = Omit<XYZ, "z"> // { x: number; y: number; }

多个属性

type T2 = Omit<XYZ, "y" | "z"> // { x: number; } 

属性有条件地

例如,所有的字符串类型:

type Keys_StringExcluded<T> = 
  { [K in keyof T]: T[K] extends string ? never : K }[keyof T]

type XYZ = { x: number; y: string; z: number; }
type T3a = Pick<XYZ, Keys_StringExcluded<XYZ>> // { x: number; z: number; }

在映射类型(PR)中使用TS 4.1键重映射/ as子句的更短版本:

type T3b = { [K in keyof XYZ as XYZ[K] extends string ? never : K]: XYZ[K] } 
// { x: number; z: number; }

字符串模式的属性

例如,排除getter(带有'get'字符串前缀的道具)

type OmitGet<T> = {[K in keyof T as K extends `get${infer _}` ? never : K]: T[K]}

type XYZ2 = { getA: number; b: string; getC: boolean; }
type T4 = OmitGet<XYZ2> //  { b: string; }

注意:TS 4.1支持上述模板文字类型。你也可以在这里写get${string}而不是get${infer _}。

更多的信息

选择,省略和其他实用程序类型

如何选择和重命名某些键使用Typescript?(重命名而不是排除)

操场上