ruk·si

🏷️ TypeScript
Intersection Types

Updated at 2024-08-02 03:11

Intersection type & combines types. Intersection types are typically used with aliased object types to create a new type that combines multiple existing object types.

type Artwork = {
    genre: string;
    name: string;
};
type Writing = {
    pages: number;
    name: string;
};
type WrittenArt = Artwork & Writing;

Avoid overusing the & types. Complex intersection types will confuse both people and even the compiler.

type NotPossible = number & string; // aka. `never`

Sources

  • Learning TypeScript by Josh Goldberg