Fixing "Conversion Of Type X May Be A Mistake"
Here we have a Dog
interface and a cat
object.
The Dog
interface is defined with a single boolean property bark
:
interface Dog {
bark: boolean;
}
We also have a cat
object whose property purr
is set to true
:
let cat = { purr: true };
Dogs bark,
Transcript
00:00 Here we have a dog interface, which has bark Boolean on it, nice and abstract. And then we have a cat here, which is saying purr true. And then we have a dog down the bottom and we're trying to assign the dog to be a dog, but we're saying cat as dog.
00:18 So we're trying to cheat TypeScript here by saying, okay, I'm gonna force this cat to be a dog. I'm gonna put a collar on it, I'm gonna make it bark or whatever, gonna stop it from purring, poor cat. But we're getting an error. Conversion of type purr Boolean to type dog may be a mistake because neither type sufficiently overlaps with the other. If this was intentional,
00:38 convert the expression to unknown first. Property bark is missing in type purr Boolean, but required in type dog. Your job is to work out why this error is occurring and what we can do to fix it. Good luck.