0
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;
};

Michal Kurz
  • 1,592
  • 13
  • 41

1 Answers1

1

You could write a helper function to combine type-guards in this way. The type annotations are a mouthful, but it works:

type TypeGuard<S, T extends S> = (x: S) => x is T;

function typeGuardUnion<T extends TypeGuard<any, any>[]>(...fs: T): TypeGuard<
    T extends TypeGuard<infer S, any>[] ? S : never,
    T extends TypeGuard<any, infer U>[] ? U : never
> {
    return ((x: any) => fs.some(f => f(x))) as any;
}

// TypeGuard<Animal, Bird | Insect>
const hasWings = typeGuardUnion(isBird, isInsect);

Playground Link

kaya3
  • 47,440
  • 4
  • 68
  • 97