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:
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:
type ID = number | string;
type Status = 'active' | 'paused' | 'closed';
let userId: ID = 'A101';
let orderStatus: Status = 'active';
orderStatus = 'deleted'; // error — not a valid StatusNote: 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.
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
| Need | Best choice |
|---|---|
| Describe an object shape | Either (interface is common) |
| Name a union or literal set | type (interfaces cannot do this) |
| Name a basic-type alias (e.g. ID = number) | type |
| Let others "extend" your shape later | interface |
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?
✍️ Practice
- Create
type Role = 'admin' | 'editor' | 'viewer'and a variable that uses it. - Make a
type Point = { x: number; y: number }and a matching object.
🏠 Homework
- Write a
type Payment = 'cash' | 'card' | 'upi'and a function that accepts a Payment and returns a message for each.