|
|
|
|
|
by brabel
1154 days ago
|
|
The problem with your TS code is that it's using covariance on a mutable generic type, which is unsafe and strict type systems would've forbidden that. To expand: TS treats `Dog[]` as a subtype of `Animal[]` because `Dog` is a subtype of `Animal`... that work if you only read values from the array... but trying to change the array, you run into trouble. Some languages let you declare covariance (reading ) and contravariance explicitly to address this issue. To my limited knowledge of TS, that's not possible in TS (as it tries to keep things simple and compatible with JS, probably). The answers in this[1] SO question explain these concepts better than I could. [1] https://stackoverflow.com/questions/27414991/contravariance-... |
|
Why would you be able to map a call to `woof` over an `Animal[]` when `Animal` doesn't implement `woof`? I don't understand how the SO link answers these questions.