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,

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