implements
Interfaces as contracts
An interface lists the members a value must have. A class with implements promises to provide every member of that interface.
interface Named { name: string; } class User implements Named { name: string = "Ada"; } console.log(new User().name);
Ada
Many interfaces
One class can implement several interfaces at once. List them after implements and provide members for each of them.
interface Printable { print(): string; } interface Savable { save(): void; } class Doc implements Printable, Savable { print(): string { return "doc"; } save(): void { console.log("saved"); } } const doc = new Doc(); console.log(doc.print()); doc.save();
doc
saved
TS checks statically
The compiler checks implements without running the code. A missing member is a build error, so broken classes never reach users.
Removing the name field below would fail the build with a clear message about the missing member.
interface Named { name: string; } class Guest implements Named { name: string = "Guest"; } console.log(new Guest().name);
Guest
Refactor safely
Interfaces make large changes safe. Add a member to the interface and the compiler points to every class that needs an update.
interface Named { name: string; greet(): string; } class User implements Named { name: string = "Ada"; greet(): string { return "Hello " + this.name; } } console.log(new User().greet());
Hello Ada
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
class User implements Namedpromise?Answer
That the class provides every member of the interface
The compiler compares the class with the contract and reports a member that is missing or wrong.
-
How many interfaces can one class implement?
Answer
Several, listed after
implementsThe list holds every contract, and the class must satisfy all of them.
-
When is the
implementscontract checked?Answer
While the project compiles, before it runs
The check is static, so a class that breaks the contract never reaches the build output.
-
What happens when a member is added to an interface?
Answer
Every implementing class that lacks it fails to compile
The compiler lists every class that needs the new member, which makes the refactor safe.
-
What does this program print?
interface Named { name: string; } class User implements Named { name: string = "Ada"; } console.log(new User().name);Answer
Ada
The field satisfies the contract, and the object holds its value.
-
What does this program print?
interface Printable { print(): string; } interface Savable { save(): void; } class Doc implements Printable, Savable { print(): string { return "doc"; } save(): void { console.log("saved"); } } const doc = new Doc(); console.log(doc.print()); doc.save();Answer
doc / saved
The print call returns the text and the save call prints its own line after it.
-
What does this program print?
interface Named { name: string; greet(): string; } class User implements Named { name: string = "Ada"; greet(): string { return "Hello " + this.name; } } console.log(new User().greet());Answer
Hello Ada
The class provides both members, and the method joins the greeting with the field.
-
What does the compiler say about this program?
interface Named { name: string; } class Guest implements Named { } console.log(new Guest());Answer
error TS2420: Class 'Guest' incorrectly implements interface 'Named'.
The interface requires a name field, and the class provides none.
-
What does the compiler say about this program?
interface Printable { print(): string; } class Doc implements Printable { print(): number { return 1; } } console.log(new Doc().print());Answer
error TS2416: Property 'print' in type 'Doc' is not assignable to the same property in base type 'Printable'.
The member exists but its return type differs, so it does not satisfy the contract.
-
What does this program print?
interface Named { name: string; } class User implements Named { name: string = "Ada"; } const named: Named = new User(); console.log(named.name);Answer
Ada
The instance fits the interface type, and only the contract members are visible through it.
Article author: Arthur Isaev