Type Aliases
A type alias gives a name to a type with the type keyword. The name is an alias, not a new type: anything assignable to the right-hand side is assignable to the name.
Detailed Explanation
TypeScript's type system is structural. Writing type UserId = string does not create a distinct UserId type. It creates a nickname for string. A string is a UserId, and a UserId is a string. If you want a type the compiler will not mix with a raw string, you need a branded type, not an alias alone.
The reason aliases exist is that types get long. A union of four object shapes, a mapped type, a function signature with three generics — none of those read well inline. A name lets you reuse the type, mention it in error messages, and export it as part of a module's public contract.
An alias can name any type the language can write:
- object shapes:
type User = { id: string; email: string } - unions and intersections:
type Id = string | number - tuples:
type Pair = [number, number] - function types:
type Cmp<T> = (a: T, b: T) => number - mapped and conditional types:
type Flags<T> = { [K in keyof T]: boolean } - template literal types:
type EventName = \on${string}``
Interfaces cannot do most of that list. That is the practical split: aliases for computed and union types, interfaces for extendable object shapes. For a plain object, either keyword works. See type vs interface for the differences that remain.
Aliases may be generic. They may refer to themselves, which is how you type a linked list or a JSON value: type Json = string | number | boolean | null | Json[] | { [key: string]: Json }. The recursion has to go through an object or array member; type T = T | string is rejected as circular in a way the compiler cannot resolve.
Aliases do not merge. A second type User = ... in the same scope is a duplicate-identifier error. That is a feature when you want a closed definition, and a limitation when you want to augment a third-party shape — that job belongs to interfaces.
Because an alias is erased, it has no runtime footprint. typeof on a value still returns the JavaScript type. You cannot write x instanceof UserId. The name exists for the compiler and for readers of the source.
Code Examples
1. Naming a union so call sites stay readable.
// 1. Naming a union so call sites stay readable.
type Status = "idle" | "loading" | "success" | "error";
function canRetry(status: Status): boolean {
return status === "error";
}
canRetry("error");
// canRetry("retrying");
// Error: Argument of type '"retrying"' is not assignable to parameter of type 'Status'.2. A generic alias for a computed type.
// 2. A generic alias for a computed type.
type Nullable<T> = T | null;
type User = { id: string; email: string };
type UserRow = { [K in keyof User]: Nullable<User[K]> };
const row: UserRow = { id: "u_1", email: null };3. Recursive alias, and why a bare alias is not nominal.
// 3. Recursive alias, and why a bare alias is not nominal.
type Json =
| string
| number
| boolean
| null
| Json[]
| { [key: string]: Json };
const payload: Json = { ok: true, retries: 3 };
type UserId = string;
function findUser(id: UserId): void {}
findUser("not-a-deliberate-id"); // compiles — UserId is just stringCommon Gotchas
`type UserId = string` does not stop you passing any string
the alias is documentation unless you brand it. See branded types if mixing identifiers is a real bug.
Aliases do not declaration-merge
two `type` declarations with the same name in one scope fail. Use `interface` when augmentation is the point.
Error messages often expand aliases
a long intersection used as an alias may still print as its members. Prefer named object types (or interfaces) at public boundaries.
Self-reference must be indirect
`type T = { next: T | null }` is fine. `type T = T[]` in some older patterns needed a wrapper object. If the compiler says the type is circular, add an object or array layer.