Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript type guard function

type Fish = { swim: () => void };
type Bird = { fly: () => void };

function isFish(pet: Fish | Bird): pet is Fish {
  return (pet as Fish).swim !== undefined;
}

const myFish: Fish | Bird = { swim: () => {} };

isFish(myFish) && myFish.swim();
 
PREVIOUS NEXT
Tagged: #typescript #type #guard #function
ADD COMMENT
Topic
Name
6+9 =