Linter · Rules
@typescript-eslint/no-duplicate-type-constituents
Disallow the same type twice in a union or intersection
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
Reported:
type Id = string | number | string;
type Name = string;
type Label = Name | string;
type Point = { x: number } & { x: number };
function greet(name?: string | undefined) {}
Not reported:
type Id = string | number;
type Point = { x: number } & { y: number };
function greet(name?: string) {}
interface User {
nickname?: string | undefined;
}
Options
| Option | Default | What it does |
|---|---|---|
ignoreUnions | false | Don't check | unions. This also allows | undefined on an optional parameter. |
ignoreIntersections | false | Don't check & intersections. |
type Id = string | number | string;
function greet(name?: string | undefined) {}