Skip to content
← Back to rules

typescript/no-duplicate-type-constituents 正确性

This rule is turned on by default when type-aware linting is enabled.
💭 This rule requires type information.
An auto-fix is available for this rule.

它的作用

此规则禁止联合类型或交集类型中的重复组成部分。

为什么这是个问题?

联合类型和交集类型中的重复组成部分没有实际用途,且会使代码更难阅读。这很可能是错误。

示例

此规则的 错误 代码示例:

ts
type T1 = "A" | "A";

type T2 = A | A | B;

type T3 = { a: string } & { a: string };

type T4 = [A, A];

type T5 = "foo" | "bar" | "foo";

此规则的 正确 代码示例:

ts
type T1 = "A" | "B";

type T2 = A | B | C;

type T3 = { a: string } & { b: string };

type T4 = [A, B];

type T5 = "foo" | "bar" | "baz";

配置

此规则接受一个配置对象,包含以下属性:

ignoreIntersections

type: boolean

default: false

是否忽略交集类型中的重复类型。 当为 true 时,允许 type T = A & A

ignoreUnions

type: boolean

default: false

是否忽略联合类型中的重复类型。 当为 true 时,允许 type T = A | A

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "rules": {
    "typescript/no-duplicate-type-constituents": "error"
  }
}
bash
oxlint --type-aware --deny typescript/no-duplicate-type-constituents

参考资料