Interface Programs
Interface Programs — purpose and prerequisites
Interfaces describe structural contracts for values and classes. An object implements an interface when it has the required members; TypeScript does not create runtime interface objects. Interfaces help decouple a reporting function from the details of each data source.
What you’ll learn
- Explain the purpose of interface programs.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
Both the class instance and the object literal satisfy the Printable shape. print depends on the label method rather than a particular class.
An interface is erased after compilation; instanceof Printable is not a valid runtime check.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
interface Printable { label(): string }
class Book implements Printable {
constructor(readonly title: string) {}
label(): string { return `Book: ${this.title}`; }
}
function print(item: Printable): string { return item.label(); }
console.log(print(new Book("Learning TypeScript")));
console.log(print({ label: () => "Note: review" }));Verified output
Book: Learning TypeScript
Note: reviewCompile: npx tsc --strict --target es2022 --module commonjs interface-programs.ts; run: node interface-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
Both the class instance and the object literal satisfy the Printable shape. print depends on the label method rather than a particular class.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Common mistake and correction
An interface is erased after compilation; instanceof Printable is not a valid runtime check.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Define an interface Priceable with a price() method and implement it for two product types.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Key idea to remember
Both the class instance and the object literal satisfy the Printable shape. print depends on the label method rather than a particular class.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.