TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS2420: class incorrectly implements interface
TS2420 means a class using implements does not satisfy the interface: a member is missing, private, or of the wrong kind. Learn to read it and fix it.
TS2420 reads Class 'Sq' incorrectly implements interface 'Shape'. and is followed by one indented line saying what is wrong, such as Property 'name' is missing in type 'Sq' but required in type 'Shape'. An implements clause is a promise that every instance of the class can be used wherever the interface is expected. The compiler checks that promise at the class declaration and reports the first member that breaks it.
implements never adds members or copies code. It only checks. You still have to write every required property and method yourself.
interface Shape { area(): number; name: string } class Sq implements Shape { // error TS2420 area(): number { return 4; } }
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
Typical second lines of the message and their meaning:
| Message line | Cause | Fix |
|---|---|---|
Property 'x' is missing in type 'C' but required in type 'I'. |
The class lacks a required member. | Add it, or make it optional in the interface. |
Property 'x' is private in type 'C' but not in type 'I'. |
Interface members are public. | Remove private/protected. |
Property 'x' is optional in type 'C' but required in type 'I'. |
x?: in the class, required in the interface. |
Make it required. |
Types of property 'x' are incompatible. |
The member exists but has the wrong type; modern compilers report this as TS2416 on the member. | Fix the type. |
Usage notes
- Only the instance side is checked. Static members and the constructor are not part of what
implementsverifies. - A class can implement several interfaces:
class A implements I, J. Conflicting requirements for the same member are reported here. - Private members cannot satisfy an interface. Interface members are public, so a
privateorprotectedimplementation is rejected (second row above). - Inference does not flow from
implements. Method parameters in the class are not contextually typed from the interface, so you must annotate them (a common surprise withnoImplicitAny). - Optional interface members do not need to appear in the class.
- Structural typing means
implementsis optional. A class that has the right members is already assignable to the interface. The clause exists to get the error at the declaration instead of at a distant call site.