CodexBloom - Programming Q&A Platform

advanced patterns when extending TypeScript class with static properties

👀 Views: 69 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
typescript classes inheritance

I'm working with an scenario when trying to extend a TypeScript class that has static properties... I have a base class `Animal` with a static property `species`, and a subclass `Dog` that should inherit this property. However, when I log the `species` from the `Dog` class, it seems to return `undefined` instead of inheriting the value from `Animal`. Here's the code I have so far: ```typescript class Animal { static species: string = 'Mammal'; } class Dog extends Animal { static bark() { console.log(`This is a ${this.species}.`); } } Dog.bark(); // Expected output: This is a Mammal. ``` Instead, I get the output: `This is a undefined.`. I've also tried accessing the `species` directly from the `Animal` class, like this: ```typescript console.log(Animal.species); // This outputs: Mammal ``` It seems that the static property is indeed defined in the `Animal` class, but when I try to access it in the `Dog` class, it fails to reflect the inherited value. I checked TypeScript's documentation on class inheritance, but I couldn't find anything that explains this behavior. Am I doing something wrong here with the static context? I'm using TypeScript 4.6.2 and targeting ES6. Any insights on this would be greatly appreciated! For context: I'm using Typescript on Ubuntu.