Why is this an issue?

In TypeScript, a type alias is a way to give a name to a specific type. It allows you to create a new name for an existing type, making your code more expressive and readable. This is especially useful when you are working with complex or lengthy types that you use frequently.

Type aliases should be preferred over complex types like unions or intersections for several reasons:

This rule enforces the rule of three for code refactoring and reports unions and intersections with three or more constituents appearing at least three times in the codebase.

function foo(x: string | null | number) { // Noncompliant: The union has three constituents and is duplicated three times in the code
  /* ... */
}

let bar: string | null | number = /* ... */;

function baz(): string | null | number {
  /* ... */
}

You should define a type alias for the union or intersection duplicated in the code and replace all their occurrences with the alias name.

type MyType = string | null | number;

function foo(x: MyType) {
  /* ... */
}

let bar: MyType = /* ... */;

function baz(): MyType {
  /* ... */
}

Resources

Documentation