Skip to main content

no-duplicate-type-constituents

Disallow duplicate constituents of union or intersection types.

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

💭

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

TypeScript 支持联合和交集类型中的类型("constituents")彼此重复。然而,开发者通常期望每个成分在其交集或并集内都是唯一的。重复的值会使代码过于冗长,并且通常会降低可读性。

¥TypeScript supports types ("constituents") within union and intersection types being duplicates of each other. However, developers typically expect each constituent to be unique within its intersection or union. Duplicate values make the code overly verbose and generally reduce readability.

该规则不允许重复的并集或交集成分。如果类型在类型系统中计算出相同的结果,我们认为类型是重复的。例如,给定 type A = stringtype T = string | A,此规则将标记 Astring 是相同类型。

¥This rule disallows duplicate union or intersection constituents. We consider types to be duplicate if they evaluate to the same result in the type system. For example, given type A = string and type T = string | A, this rule would flag that A is the same type as string.

当函数参数标记为可选时,此规则还不允许在类型联合中明确列出 undefined。这样做是不必要的。请注意,此检查仅适用于参数,不适用于属性。因此,它与 exactOptionalPropertyTypes TypeScript 编译器设置不冲突。

¥This rule also disallows explicitly listing undefined in a type union when a function parameter is marked as optional. Doing so is unnecessary. Please note that this check only applies to parameters, not properties. Therefore, it does not conflict with the exactOptionalPropertyTypes TypeScript compiler setting.

type T1 = 'A' | 'A';

type T2 = string | string | number;

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

type T4 = [1, 2, 3] | [1, 2, 3];

type StringA = string;
type StringB = string;
type T5 = StringA | StringB;

const fn = (a?: string | undefined) => {};
Open in Playground
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-duplicate-type-constituents": "error"
}
});

在线运行试试这个规则 ↗

选项

该规则接受以下选项:

type Options = [
{
/** 是否忽略 `&` 交集。 */
ignoreIntersections?: boolean;
/** 是否忽略 `|` 联合。 */
ignoreUnions?: boolean;
},
];

const defaultOptions: Options = [
{ ignoreIntersections: false, ignoreUnions: false },
];

¥Options

ignoreIntersections

是否忽略 & 交集。 Default: false.

当设置为 true 时,将忽略对交集类型成分的重复检查。

¥When set to true, duplicate checks on intersection type constituents are ignored.

ignoreUnions

是否忽略 | 联合。 Default: false.

当设置为 true 时,将忽略联合类型成分的重复检查。

¥When set to true, duplicate checks on union type constituents are ignored.

何时不使用它

¥When Not To Use It

有时,出于文档目的,包含同一类型的别名可能很有用。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

¥It can sometimes be useful for the sake of documentation to include aliases for the same type. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

在某些情况下,品牌类型 可能是一种类型安全的方式来表示底层数据类型。

¥In some of those cases, branded types might be a type-safe way to represent the underlying data types.

相关

¥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.

资源