noDuplicateTypeConstituents (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noDuplicateTypeConstituents - This rule has a safe fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
@typescript-eslint/no-duplicate-type-constituents
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noDuplicateTypeConstituents": "error" } } }}Description
Section titled “Description”Disallow duplicate types in union and intersection types.
A union type (A | B) accepts a value of any of the listed types.
An intersection type (A & B) requires a value to match all of the listed types.
Listing the same type more than once has no effect in either case.
A repeated type is usually a copy-paste mistake, and it makes the type harder to read.
Two types are duplicates when they are written the same way.
The rule ignores spaces, comments, quote styles, and parentheses around a listed type, so "A" | ('A') is reported.
The rule also looks inside parenthesized groups of the same kind, so A | (B | A) is reported.
The rule doesn’t look up what a type name refers to, so A | B isn’t reported even when B is another name for A.
The rule also reports undefined in the type of an optional parameter (a parameter marked with ?),
because an optional parameter already accepts undefined.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”type T = string | number | string;code-block.ts:1:28 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type is already listed in the union type.
> 1 │ type T = string | number | string;
│ ^^^^^^
2 │
ℹ It is first listed here.
> 1 │ type T = string | number | string;
│ ^^^^^^
2 │
ℹ Listing the same type more than once has no effect and makes the type harder to read.
ℹ Remove the duplicate type, or replace it with the type you meant.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the duplicate type.
1 │ type·T·=·string·|·number·|·string;
│ --------
type T = "a" | "b" | "a";code-block.ts:1:22 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type is already listed in the union type.
> 1 │ type T = "a" | "b" | "a";
│ ^^^
2 │
ℹ It is first listed here.
> 1 │ type T = "a" | "b" | "a";
│ ^^^
2 │
ℹ Listing the same type more than once has no effect and makes the type harder to read.
ℹ Remove the duplicate type, or replace it with the type you meant.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the duplicate type.
1 │ type·T·=·"a"·|·"b"·|·"a";
│ -----
type T = Foo & Bar & Foo;code-block.ts:1:22 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type is already listed in the intersection type.
> 1 │ type T = Foo & Bar & Foo;
│ ^^^
2 │
ℹ It is first listed here.
> 1 │ type T = Foo & Bar & Foo;
│ ^^^
2 │
ℹ Listing the same type more than once has no effect and makes the type harder to read.
ℹ Remove the duplicate type, or replace it with the type you meant.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the duplicate type.
1 │ type·T·=·Foo·&·Bar·&·Foo;
│ -----
type T = (A | B) | (A | B);code-block.ts:1:20 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type is already listed in the union type.
> 1 │ type T = (A | B) | (A | B);
│ ^^^^^^^
2 │
ℹ It is first listed here.
> 1 │ type T = (A | B) | (A | B);
│ ^^^^^
2 │
ℹ Listing the same type more than once has no effect and makes the type harder to read.
ℹ Remove the duplicate type, or replace it with the type you meant.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the duplicate type.
1 │ type·T·=·(A·|·B)·|·(A·|·B);
│ ---------
function f(a?: string | undefined) {}code-block.ts:1:25 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This undefined is unnecessary because the parameter is optional.
> 1 │ function f(a?: string | undefined) {}
│ ^^^^^^^^^
2 │
ℹ A parameter marked with ? already accepts undefined.
ℹ Remove undefined from the parameter type.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove undefined.
1 │ function·f(a?:·string·|·undefined)·{}
│ -----------
type T1 = string | number;type T2 = "a" | "b";type T3 = Foo & Bar;type T4 = (A | B) | (A & B);type T5 = Set<string> | Set<number>;function f(a: string | undefined) {}Options
Section titled “Options”ignoreIntersections
Section titled “ignoreIntersections”Default: false
When set to true, the rule doesn’t check intersection types.
{ "linter": { "rules": { "nursery": { "noDuplicateTypeConstituents": { "level": "on", "options": { "ignoreIntersections": true } } } } }}type T = Foo & Foo;Invalid
Section titled “Invalid”type T = Foo | Foo;code-block.ts:1:16 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type is already listed in the union type.
> 1 │ type T = Foo | Foo;
│ ^^^
2 │
ℹ It is first listed here.
> 1 │ type T = Foo | Foo;
│ ^^^
2 │
ℹ Listing the same type more than once has no effect and makes the type harder to read.
ℹ Remove the duplicate type, or replace it with the type you meant.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the duplicate type.
1 │ type·T·=·Foo·|·Foo;
│ -----
ignoreUnions
Section titled “ignoreUnions”Default: false
When set to true, the rule doesn’t check union types.
This also turns off the check for undefined in the type of an optional parameter.
{ "linter": { "rules": { "nursery": { "noDuplicateTypeConstituents": { "level": "on", "options": { "ignoreUnions": true } } } } }}type T = Foo | Foo;function f(a?: string | undefined) {}Invalid
Section titled “Invalid”type T = Foo & Foo;code-block.ts:1:16 lint/nursery/noDuplicateTypeConstituents FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type is already listed in the intersection type.
> 1 │ type T = Foo & Foo;
│ ^^^
2 │
ℹ It is first listed here.
> 1 │ type T = Foo & Foo;
│ ^^^
2 │
ℹ Listing the same type more than once has no effect and makes the type harder to read.
ℹ Remove the duplicate type, or replace it with the type you meant.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the duplicate type.
1 │ type·T·=·Foo·&·Foo;
│ -----
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.