There are a few questions already asked like this. But I think this a little more specific.
Please see this example:
interface Body {
legs: number;
}
interface Kingdom {
animalia: {
sound: string; // meow..
body: Body;
};
}
function GetBody<
Category extends keyof Kingdom,
B extends Kingdom[Category]["body"]
>(cat: Category): B {
// stuff..
return { legs: 4 }; // <==== Error!
}
// called like: GetBody('animalia').legs
The Error says:
Type '{ legs: number; }' is not assignable to type 'B'.
'{ legs: number; }' is assignable to the constraint of type 'B',
but 'B' could be instantiated with a different subtype of constraint 'Body'.ts(2322)
What am I doing it wrong here? How can I fix this?