ProgramGuru

Implementing Interfaces

Understand Implementing Interfaces

A class can use implements to have TypeScript verify that its instance members satisfy an interface.

What you will learn: the purpose of implementing interfaces, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how implementing interfaces works and where its type rules apply.
  • Predict the exact output of the implementing interfaces example.
  • Diagnose a common issue: Do not write new Printable(): interfaces are types only and cannot be instantiated at runtime.

Build the mental model

An interface declares a structural contract and does not become a runtime constructor. A class can implement several interfaces, and unrelated objects can satisfy the same interface by shape. implements does not generate missing methods.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Implementing Interfaces: explained visuallyPrintable contract → Invoice class → Plain object tooPrintable contractInvoice classPlain object tooFollow the values through the operation
Implementing Interfaces: Printable contract → Invoice class → Plain object too.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

interface Printable {
  print(): string;
}
class Invoice implements Printable {
  constructor(public amount: number) {}
  print(): string { return `Invoice: ${this.amount}`; }
}
function show(item: Printable): void { console.log(item.print()); }
show(new Invoice(120));
show({ print: () => "Summary" });

Verified output

Invoice: 120
Summary

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

Invoice has a compatible print method, so it satisfies Printable. The second plain object also has such a method. Both are accepted by show, which prints their returned strings.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not write new Printable(): interfaces are types only and cannot be instantiated at runtime.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Create an interface Named with getName(): string and implement it in two unrelated classes.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Why does a plain object work with show(item: Printable)?
Choose one answer. You can try again.

Answer and explanation

It structurally satisfies the print method contract.

A class can use implements to have TypeScript verify that its instance members satisfy an interface.

Review the key idea

Implementing Interfaces: A class can use implements to have TypeScript verify that its instance members satisfy an interface.

Invoice has a compatible print method, so it satisfies Printable. The second plain object also has such a method. Both are accepted by show, which prints their returned strings.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: ECMAScript Private Fields →