type Bird = { fly: () => "fly" };
type Insect = { annoy: () => "annoy" };
type Dog = { beTheBest: () => "dogdogdog" };
type Animal = Bird | Insect | Dog;
const isBird = (animal: Animal): animal is Bird => {
if ("fly" in animal) return true;
return false;
};
const isInsect = (animal: Animal): animal is Insect => {
if ("annoy" in animal) return true;
return false;
};
const hasWings = (animal: Animal) => {
if (isBird(animal)) return true;
if (isInsect(animal)) return true;
return false;
};
I am combining the two basic type-guarding functions into the composite hasWings
guard, but TypeScript does not infer it's type-guarding characteristic - it infers typeof hasWings
as simply (a: Animal) => boolean
. Is there a way I can help TS to infer, or explicitly tell that hasWings
is a combination of the isInsect
and isBird
type guards, without having to manually re-specify the criteria?
// Something like this would be useful to me:
const hasWings = (animal: Animal): ReturnType<typeof isBird> & ReturnType<typeof isInsect> => {
if (isBird(animal)) return true;
if (isInsect(animal)) return true;
return false;
};
// Having to specify the whole list manually is not useful to me:
const hasWings = (animal: Animal): animal is Insect | Bird => {
if (isBird(animal)) return true;
if (isInsect(animal)) return true;
return false;
};