Describing ShapesCore· 30 min read

Type Aliases & the type Keyword

The type keyword gives any type a reusable name — including unions that interfaces cannot express.

What you will learn

  • Create a named type with the type keyword
  • Name a union type for reuse
  • Know when to pick type vs interface

Give a type a name

The type keyword lets you create a type alias — a friendly name for any type. Think of it like saving a contact in your phone: instead of dialling the long number every time, you save it once as "Mum" and use the short name everywhere. A type alias does the same for a type.

You can name an object shape, just like an interface:

A type alias for an object shape
type User = {
  name: string;
  age: number;
};

const asha: User = { name: 'Asha', age: 21 };
console.log(asha.name);

Note: Output: Asha For an object shape, type works almost exactly like interface. Both describe what the object must contain.

Where type really shines: unions

Unlike an interface, a type can give a name to a union or a literal set — something you will reuse all over your code:

Naming a union and a literal set with type
type ID = number | string;
type Status = 'active' | 'paused' | 'closed';

let userId: ID = 'A101';
let orderStatus: Status = 'active';

orderStatus = 'deleted';     // error — not a valid Status

Note: Output: Error: Type '"deleted"' is not assignable to type 'Status'. Now Status is a reusable name. Use it anywhere instead of repeating the three options, and typos are caught everywhere.

The real payoff: write it once, reuse it

The point of a name is reuse. Once Status exists, every variable, parameter and return type can use it. If the rules ever change — say you add a fourth status — you edit one line and the whole codebase updates.

Reusing the same named type in a variable and a parameter
type Status = 'active' | 'paused' | 'closed';

function badge(status: Status): string {
  return 'Status: ' + status.toUpperCase();
}

let current: Status = 'paused';
console.log(badge(current));

Note: Output: Status: PAUSED Both current and the badge parameter share the one Status name. Without the alias you would repeat 'active' | 'paused' | 'closed' in both places — and risk them drifting apart.

type vs interface

NeedBest choice
Describe an object shapeEither (interface is common)
Name a union or literal settype (interfaces cannot do this)
Name a basic-type alias (e.g. ID = number)type
Let others "extend" your shape laterinterface

Tip: A simple rule many teams use: interfaces for object shapes, type for unions and aliases. Both are fine for objects — pick one style and stay consistent.

Watch out: A type alias is only a nickname, not a brand-new type. type ID = number | string does not invent a new kind of value — an ID is still just a number or a string. And like all types, it vanishes when compiled to JavaScript: it exists only to help the checker.

Q. Which can a type alias do that an interface cannot?

Answer: A type alias can name a union or literal set (e.g. type ID = number | string). Interfaces only describe object shapes.

✍️ Practice

  1. Create type Role = 'admin' | 'editor' | 'viewer' and a variable that uses it.
  2. Make a type Point = { x: number; y: number } and a matching object.

🏠 Homework

  1. Write a type Payment = 'cash' | 'card' | 'upi' and a function that accepts a Payment and returns a message for each.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →