As the name suggests, union types are types that we can combine together to form a new type. Unions are commonly used with string literal types, which we'll cover in the first section. Unions can be used in a pattern called discriminated union, which we can use when creating generic and reusable React components.
Union types
String literal types
A variable of a string literal type can only be assigned the exact string value specified in the string literal type.
In the TypeScript playground, let's go through an example:
- Let's create a string literal type called Control that can only be set to the "Textbox" string:
type Control = "Textbox";
- Let's now create a variable called notes...