Skip to main content

no-redundant-type-constituents

Disallow members of unions and intersections that do nothing or override type information.

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

某些类型可以在联合或交集中覆盖其他类型("constituents")和/或被其他类型覆盖。TypeScript 的类型集合论包括组成类型在父并集或交集中可能无用的情况。

¥Some types can override some other types ("constituents") in a union or intersection and/or be overridden by some other types. TypeScript's set theory of types includes cases where a constituent type might be useless in the parent union or intersection.

| 联合内:

¥Within | unions:

  • anyunknown "override" 所有其他联合成员

    ¥any and unknown "override" all other union members

  • never 从任何位置的联合中删除,除了在返回类型位置时

    ¥never is dropped from unions in any position except when in a return type position

  • 原始类型,例如 string "override" 任何文字类型,例如 ""

    ¥primitive types such as string "override" any of their literal types such as ""

& 交集内:

¥Within & intersections:

  • anynever "override" 所有其他交集成员

    ¥any and never "override" all other intersection members

  • unknown 从交集中删除

    ¥unknown is dropped from intersections

  • 文字类型 "override" 交集中的任何原始类型

    ¥literal types "override" any primitive types in an intersection

  • 文字类型,例如 "" "override" 它们的任何原始类型,例如 string

    ¥literal types such as "" "override" any of their primitive types such as string

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-redundant-type-constituents": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

type UnionAny = any | 'foo';
type UnionUnknown = unknown | 'foo';
type UnionNever = never | 'foo';

type UnionBooleanLiteral = boolean | false;
type UnionNumberLiteral = number | 1;
type UnionStringLiteral = string | 'foo';

type IntersectionAny = any & 'foo';
type IntersectionUnknown = string & unknown;
type IntersectionNever = string | never;

type IntersectionBooleanLiteral = boolean & false;
type IntersectionNumberLiteral = number & 1;
type IntersectionStringLiteral = string & 'foo';
Open in Playground

局限性

¥Limitations

此规则可防止 类型表达式用于误导性位置,例如分配给变量、作为函数参数提供或从函数返回。

¥This rule plays it safe and only works with bottom types, top types, and comparing literal types to primitive types.

选项

该规则不可配置。

何时不使用它

¥When Not To Use It

出于文档目的,某些项目有时会故意包含冗余类型成分。例如,以下代码将 string 包含在联合中,即使 unknown 使其变得多余:

¥Some projects choose to occasionally intentionally include a redundant type constituent for documentation purposes. For example, the following code includes string in a union even though the unknown makes it redundant:

/**

* Normally a string name, but sometimes arbitrary unknown data.
*/
type NameOrOther = string | unknown;

如果你强烈偏好这些不必要的类型成分,则此规则可能不适合你。

¥If you strongly feel a preference for these unnecessary type constituents, this rule might not be for you.

进一步阅读

¥Further Reading

相关

¥Related To


Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'