@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 | stringUnion type constituent is duplicated with string.;
type Name = string;
type Label = Name | stringUnion type constituent is duplicated with Name.;
type Point = { x: number } & { x: number }Intersection type constituent is duplicated with { x: number }.;
function greet(name?: string | undefinedExplicit undefined is unnecessary on an optional parameter.) {}example.tsNo problems
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. |
example.tswith
{ "ignoreUnions": true }No problemstype Id = string | number | string;
function greet(name?: string | undefined) {}