TypeScript Type Aliases

Summary: in this tutorial, you will learn how to define new names for types using type aliases.

Introduction to TypeScript type aliases

Type aliases allow you to create a new name for an existing type. The following shows the syntax of the type alias:

type alias = existingType;

The existing type can be any valid TypeScript type.

The following example uses the type alias chars for the string type:

type chars = string;
let messsage: chars; // same as string typeCode language: JavaScript (javascript)

It’s useful to create type aliases for union types. For example:

type alphanumeric = string | number;
let input: alphanumeric;
input = 100; // valid
input = 'Hi'; // valid
input = false; // Compiler errorCode language: JavaScript (javascript)

Summary

  • Use type aliases to define new names for existing types.
Was this tutorial helpful ?