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

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-duplicate-type-constituents": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-duplicate-type-constituents": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-duplicate-type-constituents

版本

此规则于 v1.12.0 中添加。

参考资料