Interfaces
An interface names an object shape: the properties, optional fields, and call signatures a value must have. Two interfaces with the same name in the same scope merge into one.
Detailed Explanation
An interface is a contract written as a list of members. Anything with those members, structurally, satisfies the interface. There is no runtime object representing the interface, and there is no implements check when the program runs. The compiler compares shapes. A class that says implements User is asking the compiler to verify the shape; the emitted JavaScript does not mention User.
interface User {
id: string;
email: string;
displayName?: string;
}
Members can be required, optional (?), or readonly. An interface can describe a function (interface Cmp { (a: number, b: number): number }), a constructable (new (url: string): Connection), and an index signature for dictionary-like objects. It can extend one or more parents with extends. Extending is a named relationship: incompatible overrides fail at the interface, not later at an assignment.
The feature that distinguishes interfaces from type aliases is declaration merging. If you write interface Window in an ambient file, and the DOM library also writes interface Window, the result is one type with both sets of members. That is how DefinitelyTyped and lib.dom let you add a field to a global. It is also how a second interface User in your own file silently grows the first one.
Interfaces cannot name unions, primitives, tuples, or mapped types. interface Id = string | number is not valid syntax. When the type you want is computed, you need a type alias. When the type you want is an object you expect others to extend or augment, an interface is the better default.
A class can implements an interface. That check is structural too: the class must have the members, but the compiler does not require the implements clause for assignability. The clause is documentation plus an early error if a method is missing. An interface can also extend a class, which pulls in the instance members of that class as a shape — useful, and easy to overuse.
Because merging is silent, treat a public interface as append-only. Removing or tightening a member is a breaking change for every file that merged or implemented it. For a closed object type that should not grow, a type alias is the stricter tool.
Code Examples
1. A shape, optional member, and a class that implements it.
// 1. A shape, optional member, and a class that implements it.
interface Account {
id: string;
email: string;
displayName?: string;
}
class Customer implements Account {
constructor(
public id: string,
public email: string,
public displayName?: string,
) {}
}
const a: Account = { id: "a1", email: "ada@example.com" };2. Extending, and a conflict reported at the interface.
// 2. Extending, and a conflict reported at the interface.
interface Timestamped {
createdAt: Date;
}
interface Bill extends Account, Timestamped {
cents: number;
}
interface Bad extends Account {
email: number;
}
// Error: Interface 'Bad' incorrectly extends interface 'Account'.3. Declaration merging combines members.
// 3. Declaration merging combines members.
interface Config {
region: string;
}
interface Config {
retries: number;
}
const cfg: Config = { region: "ap-south-1", retries: 3 };Common Gotchas
Merging is silent
a second `interface User` in the same scope adds fields instead of failing. Search for the name before declaring it, especially in a file that imports nothing.
Interfaces cannot describe unions or primitives
`interface Status { }` cannot be `"ok" | "err"`. Use a type alias for those.
`implements` is a compile-time check only
the class will not carry a runtime brand. `instanceof` against an interface is a syntax error.
Optional members are `T | undefined` on read, but omission and `undefined` differ under `exactOptionalPropertyTypes`
decide whether a field may be missing or may be present-and-undefined.