typescript 从类型中排除属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48215950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:51:37  来源:igfitidea点击:

Exclude property from type

typescript

提问by Qwertiy

I'd like to exclude a single property from the type. How can I do that?

我想从类型中排除一个属性。我怎样才能做到这一点?

For example I have

例如我有

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

And I want to exclude property zto get

我想排除财产z以获得

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

回答by CRice

For versions of TypeScript at or above 3.5

对于 3.5 或更高版本的 TypeScript

In TypeScript 3.5, the Omittype was added to the standard library. See examples below for how to use it.

在 TypeScript 3.5 中,该Omit类型被添加到标准库中。请参阅下面的示例了解如何使用它。

For versions of TypeScript below 3.5

对于低于 3.5 的 TypeScript 版本

In TypeScript 2.8, the Excludetype was added to the standard library, which allows an omission type to be written simply as:

在 TypeScript 2.8 中,该Exclude类型被添加到标准库中,允许省略类型简单地写为:

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

For versions of TypeScript below 2.8

对于低于 2.8 的 TypeScript 版本

You cannot use the Excludetype in versions below 2.8, but you can create a replacement for it in order to use the same sort of definition as above. However, this replacement will only work for string types, so it is not as powerful as Exclude.

您不能Exclude在低于 2.8 的版本中使用该类型,但您可以为它创建一个替代品,以便使用与上述相同类型的定义。但是,此替换仅适用于字符串类型,因此它不如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>>


And an example of that type in use:

以及使用该类型的示例:

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}

回答by Jason Hoetger

With typescript 2.8, you can use the new built-in Excludetype. The 2.8 release notesactually mention this in the section "Predefined conditional types":

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

Note: The Exclude type is a proper implementation of the Diff type suggested here. [...] We did not include the Omit type because it is trivially written as Pick<T, Exclude<keyof T, K>>.

注意: Exclude 类型是此处建议的 Diff 类型的正确实现。[...] 我们没有包括 Omit 类型,因为它被简单地写成Pick<T, Exclude<keyof T, K>>.

Applying this to your example, type XY could be defined as:

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

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

回答by Qwertiy

I've found solutionwith declaring some variables and using spread operator to infer type:

我找到了声明一些变量并使用扩展运算符来推断类型的解决方案

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

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

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

It works, but I would be glad to see a better solution.

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

回答by Krzysztof Kaczor

If you prefer to use a library, use ts-essentials.

如果您更喜欢使用库,请使用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: You will find lots of other useful stuff there ;)

PS:你会在那里找到很多其他有用的东西;)

回答by GonchuB

Typescript 3.5

打字稿 3.5

As of Typescript 3.5, the Omit helper will be included: TypeScript 3.5 RC - The Omit Helper Type

从 Typescript 3.5 开始,将包含 Omit 助手:TypeScript 3.5 RC - The Omit Helper Type

You can use it directly, and you should remove your own definition of the Omit helper when updating.

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

回答by CorayThan

In Typescript 3.5+:

打字稿 3.5+ 中

interface TypographyProps {
    variant: string
    fontSize: number
}

type TypographyPropsMinusVariant = Omit<TypographyProps, "variant">

回答by Andrew Zolotarev

I do like that:

我喜欢这样:

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);