Can I check a type against a union type in typescript?

If you knew some specific key for an interested Type you can do it via in operator:

function move(pet: Fish | Bird) {
  if ("swim" in pet) {
    return pet.swim(); //pet is Fish
  }
  return pet.fly(); //pet is Bird
}

As for me much easier than an additional function with stuff.
That what I was looking for.

Docs.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)