Created
February 24, 2025 09:20
-
-
Save mt3o/51ef49eea351bc799643d7fc7c7a7124 to your computer and use it in GitHub Desktop.
Typescript won't accept Array with polymorphic elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
name: string | undefined; | |
} | |
class Cat extends Animal { | |
food: string | undefined; | |
} | |
let animals: Animal[] = [ | |
{ name: "Whiskers", food: "fish" }, //won't go with TS | |
]; | |
let cat = { name: "Whiskers", food: "fish" }; //won't go with TS | |
animals.push(cat); // This should work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment