To override a getter on a subclass in TypeScript, you can define a new getter with the same name on the subclass, and use the super
keyword to call the getter of the base class. Here's an example:
index.ts242 chars18 lines
In this example, the Animal
class has a sound
getter that returns an empty string. The Dog
class extends Animal
, and overrides the sound
getter with one that returns the string 'woof'. The super
keyword is used in the implementation of the Dog
class's sound
getter to call the Animal
class's sound
getter.
When you create instances of Animal
and Dog
and access their sound
properties, you can see that the overridden sound
getter on the Dog
class returns the expected value.
gistlibby LogSnag