Category: TypeScript
Types can be composed together using the union operator or the intersection operator.
Unions are created using the | (pipe) operator. This means a value can have any valid TypeScript type in the union.
type AccountCode = number | string
Using the above, the following assignments are valid:
const firstAccountCode: AccountCode = 'abcaed34112';
const secondAccountCode: AccountCode = 4332914124;
Intersections create a completely new type that has all the properties of the types being intersected together.
Assume the following types:
type Product = {
code: string;
manufacturer: string;
description: string;
features?: string[];
};
type Supplier = {
name: string;
}
type StockDetails = {
code: string;
inventoryLevel: number;
supplier: Supplier;
}
A new type using the above types can be created with an intersection as follows:
type InventoryItem = Product & StockDetails;
let firstInventoryItem: InventoryItem = {
code: 200,
manufacturer: true,
description: 'Hula Hoop',
inventoryLevel: 37,
supplier: {
name: 'Plastics R Us'
}
}