implements implements & Classes as Types
Last updated: 2026-10-22
implements implements & Classes as Types
implements makes a class claim an interface: the compiler verifies the class provides every required member with matching types. The interface says "what it can do", the class answers "how" — one interface, many different implementations.
| Group | Functions & Classes |
|---|---|
| Kind | Syntax |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
interface Storage {
save(key: string, value: string): void;
load(key: string): string | null;
}
class MemoryStorage implements Storage {
private map = new Map<string, string>();
save(key: string, value: string): void { this.map.set(key, value); }
load(key: string): string | null { return this.map.get(key) ?? null; }
}
⚙️ Parameters
| implements | Claims interfaces: several at once allowed (implements A, B); the check is "class satisfies interface" |
|---|---|
| extends + implements | Both may be combined: class Foo extends Base implements Storage — inherit implementation while honoring the contract |
Returns:Method return types must match the interface or be narrower (covariant); parameters must be wider (contravariant) — otherwise a compile error.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
dark null
(Runtime output after transpilation; deleting load or widening its return type to string is a compile error — implements checks member by member)
💡 Notes
ℹ️ Info implements is a compile-time check with no runtime effect; after transpilation the interface is gone and the class is a plain class.
⚠️ Note Interfaces describe public members only — private fields are not part of the check; conversely every interface member must exist on the class, no exceptions.
💡 Tip This is dependency inversion in practice: business code depends on the Storage interface, tests inject MemoryStorage, production injects LocalStorage — caller code never changes.
🏷️ Related Functions & Classes
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q implements vs extends?
A extends inherits implementation (takes code); implements promises a shape (takes a contract). A class extends one base but may implement many interfaces.
Q Can a class be assigned to an interface type without implements?
A Yes — TS is structural, shape alone suffices. implements adds explicit intent and surfaces "interface changed, class didn't" at the class definition instead of at every assignment site.
Q Can an interface constrain a constructor?
A Yes, via a new signature: interface Ctor<T> { new (...args: any[]): T } — common for factory and container registration types.