Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript property does not exist on union type

type Fish = { swim: () => void };
type Bird = { fly: () => void };
 
function move(animal: Fish | Bird) {
  if ("swim" in animal) {
    return animal.swim();
  }
 
  return animal.fly();
}
Try
Source by www.typescriptlang.org #
 
PREVIOUS NEXT
Tagged: #typescript #property #exist #union #type
ADD COMMENT
Topic
Name
8+4 =