JavaScript

JavaScript

Made by DeepSource

Prefer having type alias for all union and intersection types JS-0493

Type check
Minor

It is recommended to make type aliases for all union and intersection types. If these are used in raw forms, it might be tempting to copy & paste them around the code. However, this brings source code pollution and unnecessary changes on several parts when these compound types need to be changed. Having a type alias also makes the type reusable without copy-pasting them. This works on the same principle of using a function in any programming language. We create a function that we think needs to be used more than once or make the code more functional and readable.

Bad Practice

function func(bar: "A" | "B") {}

const data: "A" | "B" = "A";

type DataType = { bar: "A" | "B" };

Recommended

type DType = "A" | "B";

function func(bar: DType) {}

const data: DType = "A";

type DataType = { bar: DType };