I have this code sample:
type Dog = {
legs?: number;
};
function printDogLegs(dog: Dog) {
if (dog.legs !== undefined) {
printConfirmedDog(dog);
}
}
type ConfirmedDogWithLegs = {
legs: number;
};
function printConfirmedDog(input: ConfirmedDogWithLegs) {
console.log(input);
}
Despite the typeguard, I get this error from the TypeScript compiler:
TS2345: Argument of type 'Dog' is not assignable to parameter of type 'ConfirmedDogWithLegs'.
Types of property 'legs' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
212 | function printDogLegs(dog: Dog) {
213 | if (dog.legs !== undefined) {
> 214 | printConfirmedDog(dog);
| ^^^
215 | }
216 | }
217 |
I would expect TS to narrow the type of Dog from legs: number | undefined to legs: number. Why is this not the case? And how should I go about solving this problem?