@typescript-eslint/no-duplicate-type-constituents

Disallow the same type twice in a union or intersection

fixableneeds types

Also called typescript-eslint/no-duplicate-type-constituents, typescript/no-duplicate-type-constituents

string | number | string means the same as string | number. The repeat adds nothing, and in a long union it hides what the type really allows.

The rule compares types, not only text, so it also finds an alias for a type that's already in the union. It also reports | undefined on an optional parameter, where the ? already says it.

--fix removes the repeated type.

bun lint --type-aware turns this rule on in TypeScript files.

Configure#

Off by default. This rule needs types, so turning it on makes bun lint type check your code first.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-duplicate-type-constituents" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-duplicate-type-constituents" = ["error", {
  ignoreUnions = false,
  ignoreIntersections = false,
}]

Examples#

example.ts4 problems
type Id = string | number | string;

type Name = string;
type Label = Name | string;

type Point = { x: number } & { x: number };

function greet(name?: string | undefined) {}
example.tsNo problems
type Id = string | number;

type Point = { x: number } & { y: number };

function greet(name?: string) {}

interface User {
  nickname?: string | undefined;
}

Options#

OptionDefaultWhat it does
ignoreUnionsfalseDon't check | unions. This also allows | undefined on an optional parameter.
ignoreIntersectionsfalseDon't check & intersections.
example.tswith { "ignoreUnions": true }No problems
type Id = string | number | string;

function greet(name?: string | undefined) {}