×

typescript · level 2 · unit 1 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a nickname for any type you like.

Write type, a name, = and a type: type Pounds = number;. Now Pounds means number everywhere, but your code also says what the number is for.

An alias can name an object shape too: type Point = { x: number; y: number };. For objects, an alias and an interface do nearly the same job. Many teams use interfaces for objects and aliases for everything else. Pick one style and stick with it.

Both are strict about extras. Put a property in a new object that the shape does not list and the checker says TS2353. That catches typos in property names.

type Pounds = number;
type Point = { x: number; y: number };
const ticket: Pounds = 4.5;
const home: Point = { x: 2, y: 5 };
console.log(`A ticket costs £${ticket}.`);
console.log(`Home is at ${home.x}, ${home.y}.`);

An alias never makes a new kind of value. Pounds is still a plain number.