Extends and implements
Concept and syntax
extends inherits implementation and members from a base class. implements checks that a class instance satisfies an interface but does not add fields, infer method parameter types, or supply code. A class may implement several interfaces but has one direct superclass.
Working example
Save each snippet separately as main.ts in a fresh directory. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts, then run the accepted example with node main.js. Forced module detection isolates snippet names from global declarations.
interface Named {label:string;}class Base {count=7;}class Item extends Base implements Named {label="tea";}const item=new Item();console.log(item.label,item.count);Expected output:
tea 7Common mistake
Intentionally invalid; compile separately and do not execute.
interface Named {label:string;}class Item implements Named {}Expected diagnostic: TS2420. implements does not generate the missing field. Declare and initialize label.
Practice and recap
Add a second interface requiring a read method and implement it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Extends and implements — knowledge check
Check the contract, trace output, and diagnose an invalid example.
Which statement about this lesson’s concept is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
interface Named {label:string;}class Base {count=7;}class Item extends Base implements Named {label="tea";}const item=new Item();console.log(item.label,item.count);Which diagnostic and explanation match the rejected example under the lesson settings?
interface Named {label:string;}class Item implements Named {}