A Class Promising to Match an Interface
`implements`
`implements` declares that a class conforms to an interface. TS checks the shape — but the interface doesn't inject anything.
What you'll learn
- Use `implements` to assert shape conformance
- Implement multiple interfaces
- Know the difference between `implements` and `extends`
implements tells TS: “this class promises to satisfy these
interfaces.” TS then checks every required field and method.
Single Interface
interface Logger {
log(message: string): void;
}
class ConsoleLogger implements Logger {
log(message: string) {
console.log(`[log] ${message}`);
}
} Forget the log method and TS errors — class 'ConsoleLogger' incorrectly implements interface 'Logger'.
Multiple Interfaces
interface Logger { log(msg: string): void; }
interface Warner { warn(msg: string): void; }
class FullLogger implements Logger, Warner {
log(m: string) { console.log(m); }
warn(m: string) { console.warn(m); }
} A class can implement any number of interfaces.
implements vs extends
| Keyword | What it does |
|---|---|
extends | Inherit from a class (one parent) |
implements | Conform to an interface(s) |
class A {}
interface I { hello(): string; }
class B extends A implements I {
hello() { return "hi"; }
} extends carries over actual code from the parent. implements
brings nothing — it’s a shape check.
Watch Out — implements Doesn’t Widen
interface Greeter {
greet(name: string): void;
}
class C implements Greeter {
greet(name) { // ✗ Parameter 'name' implicitly has an 'any' type
console.log(name);
}
} implements doesn’t give the class the interface’s parameter
types — you still annotate them. It only verifies them.
Up Next
Parameter properties — a one-line shortcut for “constructor parameter + field assignment”.
Parameter Properties →