Basic · 6 of 13 · 12 min read
Type aliases
A name for any type at all — not just object shapes — and the two things that genuinely separate an alias from an interface.
- Type aliases
A type alias gives a name to a type. Any type — an object shape, a union, a tuple, a function
signature, or just string. An interface can only name an object-like shape, and that is the first
of exactly two real differences between them.
Naming anything
type ID = string | number; // a union
type Point = [number, number]; // a tuple
type Name = string; // a primitive alias
type Callback = (data: string) => void; // a function signature
type User = { id: ID; name: Name }; // an object shape
const id: ID = 123;
const point: Point = [0, 0];
const log: Callback = (data) => console.log(data);
console.log(id, point, typeof log);
An interface can express only the last of those:
interface UserInterface { id: string | number; name: string }
// interface ID = string | number; // not expressible
Note that an alias is a synonym, not a new type. type Name = string creates no distinction
the compiler will enforce:
type UserId = string;
type PostId = string;
function loadUser(id: UserId) { return id; }
const postId: PostId = "p_1";
console.log(loadUser(postId)); // "p_1" — no error; they are both just string
Making them genuinely distinct needs a brand — an intersection with a phantom property nothing can produce by accident:
type UserId = string & { readonly __brand: "UserId" };
const asUserId = (value: string) => value as UserId;
function loadUser(id: UserId) { return id; }
console.log(loadUser(asUserId("u_1"))); // "u_1"
// loadUser("u_1"); // a plain string is not assignable
Merging, and why it matters
Declare an interface twice and the declarations combine. Declare an alias twice and it is an error.
interface Animal { name: string }
interface Animal { age: number }
const a: Animal = { name: "Rex", age: 3 }; // both are required
type Pet = { name: string };
// type Pet = { age: number }; // Duplicate identifier 'Pet'
type Dog = Pet & { age: number }; // intersection instead
const d: Dog = { name: "Rex", age: 3 };
console.log(a.age, d.age);
Merging is not a quirk — it is the feature that lets you add to a type you do not own:
// Adding a property to a global, from your own file:
declare global {
interface Window {
myApp?: { version: string };
}
}
export {};
Which is the real decision rule. A library publishing types people may want to augment should use interfaces. An application that would be surprised by a second declaration of the same name is usually better off with aliases, where a duplicate is an error.
Extending versus intersecting
Both combine shapes, and they differ in where a conflict is reported:
interface Base { id: number }
// interface Bad extends Base { id: string }
// Interface 'Bad' incorrectly extends 'Base' — reported right here
type Alias = Base & { id: string }; // no error at the declaration...
declare const value: Alias;
console.log(typeof value.id);
// ...but `id` is `never`, so nothing can ever be assigned:
// const x: Alias = { id: 1 }; // not assignable
// const y: Alias = { id: "1" }; // also not assignable
extends checks as it goes; & just combines and lets the impossibility surface later, at a
confusing distance from the cause.
type |
interface |
|
|---|---|---|
| Object shapes | yes | yes |
| Unions, tuples, primitives | yes | no |
| Declared twice | error | merges |
| Combining | & |
extends (checked) |
| Implemented by a class | yes | yes |
| Computed/mapped forms | yes | no |
| Error messages | can be long and inlined | usually shows the name |
Aliases are where the type-level work happens
Everything beyond a plain shape needs an alias, because interfaces cannot be computed:
interface User { id: number; name: string; email: string }
type PublicUser = Omit<User, "email">;
type UserKey = keyof User; // "id" | "name" | "email"
type Nullable<T> = { [K in keyof T]: T[K] | null };
type Unwrap<T> = T extends Promise<infer V> ? V : T;
const u: PublicUser = { id: 1, name: "Ana" };
const k: UserKey = "name";
console.log(u.name, k);
Mapped types, conditional types and anything built with keyof or infer are alias-only
territory. That alone settles a lot of “which should I use?” arguments: you will need aliases
regardless.
Generic aliases
Aliases take type parameters, with defaults if you want them:
type Result<T, E = Error> =
| { ok: true; value: T }
| { ok: false; error: E };
type Dictionary<V> = Record<string, V>;
function parse(input: string): Result<number> {
const n = Number(input);
return Number.isNaN(n)
? { ok: false, error: new Error("not a number") }
: { ok: true, value: n };
}
const r = parse("42");
console.log(r.ok ? r.value + 1 : r.error.message); // 43
Result is the clearest argument for aliases: it is a union, so an interface could not express it
at all.
A working rule
- Union, tuple, primitive alias, or anything computed →
type. There is no choice. - An object shape a consumer or a class may extend →
interface. - A plain object shape inside an application → either. Pick one and be consistent; most teams
land on
interfacefor shapes andtypefor the rest.
// Both of these are fine, and structurally identical:
interface PropsA { title: string; onClose: () => void }
type PropsB = { title: string; onClose: () => void };
const a: PropsA = { title: "x", onClose: () => {} };
const b: PropsB = a;
console.log(b.title);
Common mistakes
Expecting an alias to create a distinct type
Mistake. type UserId = string and assuming a PostId cannot be passed.
Why it fails. An alias is a synonym. The compiler sees two names for string.
What to do instead. Brand it, if the distinction matters enough to pay for the casts at the
boundary.
Combining shapes with & and being surprised by never
Mistake. type A = Base & { id: string } where Base.id is a number.
Why it fails. The intersection is computed without complaint, and id becomes never — so
the error appears wherever someone tries to build the value.
What to do instead. interface A extends Base, which reports the conflict at the declaration.
Arguing about it
Mistake. Spending a code review on type versus interface for a plain object shape.
Why it fails. For that case they are interchangeable, and the two differences that matter —
merging and non-object types — decide themselves.
What to do instead. Follow the file you are in.
Check yourself
3 questions from the bank on what you just read. Answering here does not affect your quiz history.
What can a type alias describe that an interface cannot?
A type alias introduces a name, and names in the same scope have to be unique.
Why does this fail while the same thing with interface would not?
type Animal = { name: string };
type Animal = { age: number };Which should you reach for by default, interface or type?
Further reading
Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.
Reaching the end of a lecture marks it as read.