TypeScript Reference ยท Keywords and Declarations
TypeScript implements clause
Reference for the TypeScript implements clause: checking that a class satisfies an interface or type, multiple interfaces, what it does not do, and common errors.
The implements clause asks the compiler to check that a class has everything an interface (or object type) requires. It is a pure compile-time assertion: it adds no code, does not inherit any members and does not change the type of the class.
Because TypeScript is structural, a class does not need implements to be usable where an interface is expected. The clause is there to catch mistakes early, at the class itself rather than at a distant call site, and to document intent.
interface Printable { print(): void; } class Report implements Printable { print() { console.log("report"); } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
class C implements I1, I2 { /* members */ } class D extends Base implements I { /* members */ }
| Part | Description |
|---|---|
I1, I2 |
Interfaces, object type aliases, or class types. Any type whose members are statically known works; unions and mapped types with unresolved keys do not. |
Combined with extends |
Write extends first, then implements. |
Errors
- A missing or incompatible member: error TS2420, with details such as "Property 'area' is missing in type 'Square' but required in type 'Shape'".
- An optional interface member may be left out, but if you declare it, its type must be compatible.
- An interface with private or protected members (taken from a class) can only be implemented by that class or its subclasses.
Usage notes
- No inheritance. Members are not copied. Even a property that the interface declares must be written out in the class. The class type does not pick up its type annotation from the interface, so parameters in methods are not contextually typed (they can become implicit
any). - Method parameters are compared loosely. Interface members written as methods are checked bivariantly, so a class method with a wider or narrower parameter type can still pass. Members written as function-typed properties get strict checking under
strictFunctionTypes. - Public side only.
implementschecks the instance side. To check static members, use a separate type andsatisfies, or assert on the constructor. - Erased entirely. No
instanceoffor interfaces; use a type guard, or a class if you need a runtime check. - Interfaces can extend classes, and classes can
implementsanother class, treating it as an interface. This is rarely a good idea unless the class is purely a shape. - Readonly mismatches are not detected: a class may implement a
readonlyproperty as mutable. - Index signatures in an interface require the class to have a compatible index signature too.