Type Alias — TypeScript

type AliasName = ...; gives a name to any type: type ID = string | number;, type Point = { x: number; y: number };. The name can be used on variables and…

The number 0812-3456-7890 is hard to remember, so you save it on your phone as "Mum". From then on you just tap "Mum", and the phone calls the same number. If the number changes, you update the contact once, and every call follows the new number.

A type alias does the same for types. With the keyword type, you give a name to any type. You write the type once, then use its name everywhere.

Give a type a name

type ID = string | number;

let userCode: ID = "U-001";
let orderCode: ID = 1024;
let active: ID = true; // error
Variable Value Type of the value Fits string | number? What TypeScript says
userCode "U-001" string yes passes
orderCode 1024 number yes passes
active true boolean no Type 'boolean' is not assignable to type 'ID'.

That message means: a boolean value can't go into ID. In the editor, the message ends with the code ts(2322). That code is the error's number: the same message always has the same number, so it's easy to look up.

Change it once, everything follows

Now the alias is changed to type ID = string;. You don't touch the other lines at all, but TypeScript checks all of them again:

type ID = string;

let userCode: ID = "U-001"; // passes
let orderCode: ID = 1024;   // Type 'number' is not assignable to type 'string'.

One line changed, and TypeScript points at every place that no longer fits. That's what an alias is for: if the same type is used in 20 places, you only change one line. Notice too that the message says string, not ID. An alias for a single basic type is shown as what it stands for.

An alias for an object shape

An alias can also name an object shape, which you know from the object types lesson:

type Point = { x: number; y: number };

let start: Point = { x: 0, y: 0 };
let end: Point = { x: 10 }; // error

type or interface?

In the last lesson you used interface for object shapes. How it differs from type:

type interface
Object shape yes: type Point = { x: number; y: number }; yes: interface Point { x: number; y: number }
A union, like string | number yes no
The same name written twice error Duplicate identifier 'Point'. merged into one

interface always needs curly braces after its name, so it can only describe an object shape. Try writing interface ID = string | number;, and TypeScript stops at the = sign with the error '{' expected. (ts(1005)). So for a union, a basic type, or anything that isn't an object, use type. For an object shape, both are fine; follow your team's or project's habit. If there is no habit yet, the TypeScript docs suggest: use interface until you need something only type can do, such as a union.

An alias is just a name, not a new type

type Cents = number;
type Dollars = number;

let price: Cents = 1500;
let inDollars: Dollars = price; // no error

TypeScript accepts it. Cents and Dollars are both just other names for number, so to TypeScript they are the same type. An alias doesn't make a separate new type. If you need TypeScript to tell two number things apart, an alias alone isn't enough.

An alias doesn't make it into the JavaScript either. When TypeScript code is turned into JavaScript, every type line and every : Cents marker is removed:

let price = 1500;
let inDollars = price;

An alias on function parameters

An alias can be used on a function's parameters and return, just like a normal type:

type Size = number;

function squareArea(side: Size): number {
  return side * side;
}

console.log(squareArea(5)); // 25

In the exercise, the function getArea(shape, size) picks its formula from what shape holds:

Call Formula Result
getArea("square", 5) 5 * 5 25
getArea("square", 4) 4 * 4 16
getArea("circle", 3) Math.PI * 3 * 3 = 28.27..., then Math.round 28

Math.round(...) rounds to the nearest whole number, so 28.27 becomes 28. A square's area is already a whole number, so it doesn't need rounding.

⚠️ A missing = and an alias mistaken for a new type

TL;DR: type AliasName = ...; gives a name to any type: type ID = string | number;, type Point = { x: number; y: number };. The name can be used on variables and parameters, and if what it stands for changes, every place that uses it is checked again. A value that doesn't fit is rejected with an error like Type 'boolean' is not assignable to type 'ID'. A union can only be named with type, not interface. An alias is just another name, not a new type, and it disappears when the code is turned into JavaScript.

⚡ Give a type a name

Write:

type ID = string | number;

let userCode: ID = "U-001";
let orderCode: ID = 1024;
let active: ID = true; // error

Meaning: type ID = ... makes a new name for the type on the right of =. string | number means a string or a number, so true is rejected. Change what the alias holds, and every place that uses it is checked again.

⚡ A union needs type

Guess first: does interface ID = string | number; work?

No. interface always needs curly braces, so TypeScript stops at the = with the error '{' expected.

Meaning: For a union, a basic type, or anything that isn't an object, use type. For an object shape, type and interface both work.

⚡ An alias is just another name

Write:

type Cents = number;
type Dollars = number;

let price: Cents = 1500;
let inDollars: Dollars = price;

Meaning: The last line is not an error. Cents and Dollars are both just other names for number, so an alias doesn't make a separate new type. When the code becomes JavaScript, every type line is removed.

What you will learn