In unions and intersections, redundant types should not be used.
Why is this an issue?
When defining a union or intersection in TypeScript, it is possible to mistakenly include type constituents that encompass other constituents, that
don’t have any effect, or that are more restrictive. For instance,
- The type
something
in any | something
is redundant because any
covers all possible types, whatever
something
is.
- The types
never
in unions like never | something
or unknown
in intersections like unknown &
something
are effectless.
- More restrictive types in intersections like the literal type
1
in 1 & number
reduce the set of possible values
to specific ones.
Eliminating redundant types from a union or intersection type simplifies the code and enhances its readability. Moreover, it provides a clearer
representation of the actual values that a variable can hold.
How to fix it
The redundant and overridden types should be removed.
Code examples
Noncompliant code example
type UnionWithAny = any | 'redundant'; // Noncompliant
type UnionWithNever = never | 'override'; // Noncompliant
type UnionWithLiteral = number | 1; // Noncompliant
type IntersectionWithAny = any & 'redundant'; // Noncompliant
type IntersectionWithUnknown = string & unknown; // Noncompliant
type IntersectionWithLiteral = string & 'override'; // Noncompliant
Compliant solution
type UnionWithAny = any;
type UnionWithNever = never;
type UnionWithLiteral = number;
type IntersectionWithAny = any;
type IntersectionWithUnknown = string;
type IntersectionWithLiteral = 'override';
Resources
Documentation