Skip to content

Instantly share code, notes, and snippets.

@mt3o
Created February 24, 2025 09:20
Show Gist options
  • Save mt3o/51ef49eea351bc799643d7fc7c7a7124 to your computer and use it in GitHub Desktop.
Save mt3o/51ef49eea351bc799643d7fc7c7a7124 to your computer and use it in GitHub Desktop.
Typescript won't accept Array with polymorphic elements
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