|
|
|
|
|
by noway421
688 days ago
|
|
Here's a simpler repro: type Bird = { id: number };
type Human = { swim?: () => void; id: number };
function move(animal: Bird | Human) {
onlyForHumans(animal);
}
function onlyForHumans(swimmer: Human) {
if (swimmer.swim) {
swimmer.swim();
}
}
const someObj = { id: 1, swim: "not-callable" };
move(someObj);
`someObj` gets casted as `Bird`, then `animal` gets casted as `Human`. Unfortunately unions here are indeed unsound.As as workaround you could add a property `type` (either `"bird"` or `"human"`) to both `Bird` and `Human`, then TypeScript would be able to catch it. |
|