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.