Interface extension
Concept and syntax
An interface can extend another interface to reuse its required fields. interface Product extends Identified inherits the id contract and adds label. A value assigned to Product must satisfy both parts. Extension does not copy runtime fields or create a JavaScript subclass; it describes a combined static shape. An extended property cannot contradict the base contract.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
interface Identified { id: number; }interface Product extends Identified { label: string; }const product: Product = { id: 4, label: "tea" };const base: Identified = product;console.log(base.id, product.label);Expected output (one line per log):
4 teaCommon mistake
Intentionally invalid; check this snippet separately. Do not run it.
interface Identified { id: number; }interface Product extends Identified { id: string; }Expected compiler diagnostic: TS2430. The derived id conflicts with the inherited numeric id. Keep a compatible id or design a different base contract.
Practice and recap
Extend an interface with a new required field. Build a valid record and test a declaration that contradicts a base property.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Interface extension — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
What does Product inherit from Identified?
Can an extending interface replace number id with string id?
Which derived id type is compatible with the base?
interface Identified { id: number; }interface Product extends Identified { id: string; }