The type alias
You can create a custom name for a type using a type alias. Here's an example where we create our own type alias for the union type string | number:
We completely decide on the name StringOrNumber
. We could have chosen any other name.
Using UpperCamelCase (also known as PascalCase) as a naming convention for type aliases is recommended.
The syntax is the keyword type
followed by any name you would like, then an = (equal sign), and then the type you'd like to represent.
Benefits
- it's useful when the same type is repeated.
- This also improves your editor autocomplete experience as the editor will show you the descriptive name that you chose; for example,
StringOrNumber
when writing the parameter for convertNumber
.