Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript narrowing object

type Fish = { swim: () => void };
type Bird = { fly: () => void };
 
function move(animal: Fish | Bird) {
  if ("swim" in animal) { // in operator narrowing
    return animal.swim();
  }
  return animal.fly();
}
 
PREVIOUS NEXT
Tagged: #typescript #narrowing #object
ADD COMMENT
Topic
Name
5+2 =