Harry Yates

I'm a web developer focused on TypeScript, React, and Three.js.

The type alias

Mon, 19th Feb 2024

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:

Post

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.

Post
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.