In unions and intersections, redundant types should not be used.
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,
something in any | something is redundant because any covers all possible types, whatever
something is. never in unions like never | something or unknown in intersections like unknown &
something are effectless. 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.
The redundant and overridden types should be removed.
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
type UnionWithAny = any; type UnionWithNever = never; type UnionWithLiteral = number; type IntersectionWithAny = any; type IntersectionWithUnknown = string; type IntersectionWithLiteral = 'override';