Type aliases

The type keyword

A type alias gives a reusable name to any type. Define it once with the type keyword, then use the name everywhere instead of repeating the full spelling:

type UserId = string; const id: UserId = "u-42"; console.log(id);

u-42

UserId reads as one clear idea across the codebase. When the underlying type changes later, a single alias edit updates every usage at once.

Compose aliases

Aliases combine into larger shapes. Small names slot into object fields, so complex types grow from simple named parts:

type UserId = string; type User = { id: UserId; name: string; }; const tom: User = { id: "u-1", name: "Tom" }; console.log(tom.name);

Tom

The User alias reuses UserId for its id field. Building types from named blocks keeps each definition short and easy to review.

Alias vs interface preview

Interfaces describe object shapes too, and the next lesson covers them in full. For now, notice that both spellings below accept the same objects:

interface Admin { id: string; level: number; } const boss: Admin = { id: "a-1", level: 5 }; console.log(boss.level);

5

As a rule of thumb, either form fits plain object shapes. Aliases additionally cover unions and single primitives, which the coming lessons explore.

Naming with Type suffix

A Type suffix marks a name as type level, apart from values that exist at run time. Readers spot the role at once, even in long files:

type StatusType = "on" | "off"; const status: StatusType = "on"; console.log(status);

on

StatusType can only ever be one of two texts, so typos fail the compile. Adopt the suffix across the project and every type name tells its own story.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the type keyword do?

    Answer

    It gives a reusable name to a type

    The alias is a name for a type that the compiler expands wherever the name is used.

  2. Which kinds of type can a type alias name?

    Answer

    Any type, including primitives, unions and objects

    An alias fits any type, which is why it also covers a single primitive or a union of literals.

  3. What does this program print? type UserId = string; const id: UserId = "u-42"; console.log(id);

    Answer

    u-42

    UserId is another name for string, so the constant holds and prints the text.

  4. What does this program print? type UserId = string; type User = { id: UserId; name: string; }; const tom: User = { id: "u-1", name: "Tom" }; console.log(tom.name);

    Answer

    Tom

    The object field holds the name, and the alias of the id field does not change it.

  5. What does this program print? interface Admin { id: string; level: number; } const boss: Admin = { id: "a-1", level: 5 }; console.log(boss.level);

    Answer

    5

    The object fills both fields, and level holds the number 5.

  6. What does the compiler say about this program? type StatusType = "on" | "off"; const status: StatusType = "onn"; console.log(status);

    Answer

    error TS2322: Type '"onn"' is not assignable to type 'StatusType'.

    The union allows only the two spelled texts, so the misspelled one is a type error.

  7. Does a type alias exist while the program runs?

    Answer

    No, it is erased with the other type notes

    Types live in the source only, so the emitted JavaScript carries no alias.

  8. What does this program print? type Point = { x: number; y: number }; const p: Point = { x: 1, y: 2 }; console.log(p.x + p.y);

    Answer

    3

    The object fills both number fields, so the sum is 3.

  9. What does the compiler say about this program? type User = { id: string; name: string }; const u: User = { id: "u-1" }; console.log(u.name);

    Answer

    error TS2741: Property 'name' is missing in type '{ id: string; }' but required in type 'User'.

    The alias requires both fields, and an object without name does not match it.

  10. Which rule of thumb does the lesson give for aliases and interfaces?

    Answer

    Either fits a plain object shape; aliases also name unions and primitives

    For object shapes the two forms are close, while a union or a single primitive needs an alias.

Article author: Arthur Isaev