'How to change the typescript type of object within array on inherited class?
Link to code on TypeScript Playground
I want to be able to store type-safe instances of "Red" inside the "Box." Switching abstract value: { [index: number]: Color } to abstract value: { [index: number]: Any } prevents the error bellow, however it is not type-safe.
abstract class Color {
constructor () {}
}
class Red extends Color {
constructor () {
super()
}
doesNotWork() {
return true
}
}
abstract class Container {
abstract value: { [index: number]: Color }
constructor () {}
values () {
return Object.values(this.value)
}
}
class Box extends Container {
value: { [index: number]: Red }
constructor () {
super()
this.value = []
}
}
const wall = new Box()
wall.values().forEach((varibleObject: Red) => {
varibleObject.doesNotWork()
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
