Sub topic 15
Choosing Between Interface and Type Alias
Assume you know basic TypeScript annotations, JavaScript objects and functions, and how to run the TypeScript compiler. Interfaces and type aliases overlap heavily. Both can describe object shapes, optional properties, readonly properties, functions, and generic types. The choice usually depends on the kind of type you are modeling and how the type may evolve.
Use an interface for extendable object contracts
An interface is usually a good default for the public shape of an object. It clearly describes an object contract and works naturally with extends, classes, and declaration merging.
interface User {
id: number;
name: string;
}
interface Admin extends User {
permissions: string[];
}
class StaffUser implements User {
constructor(
public id: number,
public name: string,
) {}
}
const admin: Admin = {
id: 1,
name: "Ada",
permissions: ["manage-users"],
};Choose an interface when:
- The type describes an object that other types may extend.
- A class should implement the contract.
- A library may need declaration merging to add members later.
- Your team uses interfaces for named object contracts.
Interfaces can merge when declarations have the same name:
interface Settings {
host: string;
}
interface Settings {
port: number;
}
const settings: Settings = {
host: "localhost",
port: 8080,
};This can help library authors, but it can also cause surprises. An unrelated declaration with the same name can silently change the final shape. Avoid relying on merging in application code unless the behavior is deliberate.
Use a type alias for type composition
A type alias gives a name to any TypeScript type, not only an object shape. It is the necessary choice for unions, literal types, tuples, and many conditional or mapped types.
type RequestState = "idle" | "loading" | "success" | "error";
type ApiResult =
| { ok: true; data: string }
| { ok: false; message: string };
type WithTimestamps = {
createdAt: Date;
updatedAt: Date;
};
type RecordWithTimestamps = User & WithTimestamps;Choose a type alias when:
- You need a union or literal type.
- You need a tuple or another non-object type.
- You are combining existing types with an intersection.
- You are deriving a type with a mapped or utility type.
- You want to prevent accidental declaration merging.
An intersection type, written with &, requires a value to satisfy every component. This differs from interface extension mainly in syntax and organization. extends communicates an object inheritance relationship, while & expresses type composition.
Compare the choices
| Need | Suitable choice | Reason |
|---|---|---|
| Public object contract | Interface | Clear extension and implementation model |
| Union of alternatives | Type alias | Aliases can name unions directly |
| Class contract | Interface | Classes can use `implements` |
| Mapped or utility type | Type alias | Aliases naturally express derived types |
| Prevent same-name merging | Type alias | Repeated aliases are compiler errors |
Both constructs support generics:
interface Box<T> {
value: T;
}
type Pair<A, B> = [A, B];
const box: Box<number> = { value: 42 };
const pair: Pair<string, number> = ["age", 30];The compiler still uses structural typing. A value matches either construct when it has the required compatible members. The declared name does not make the type nominal.
Exercise
You need to model a value that is either a successful response with data or a failed response with error. Should you use an interface or a type alias, and why?
Show answer
Use a type alias because it can directly name a union of object shapes. A discriminant such as ok: true or ok: false can help TypeScript narrow the result.