unionType

mail@pastecode.io avatar
unknown
typescript
2 years ago
1.0 kB
4
Indexable
Never
interface sazan { location: string;  doSomethingFishes(someFish: Fishes): any;};
interface hamsi { length: number; doSomethingFishes(someFish: Fishes): any;};
type Fishes = sazan | hamsi;


function doSomethingFishes(someFish: Fishes): void {
    console.log("this is a some fish: ", someFish);
}

interface cheetah { speed: number; doSomethingCats(someCat: Cats): void; };
interface tiger { stripeCount: number; doSomethingCats(someCat: Cats): void; };
type Cats = cheetah | tiger;
function doSomethingCats(someCat: Cats): void;

function doSomethingCats(someCat: Cats): void {
    console.log("this is a some cat: ", someCat);
}

type Animals = Fishes | Cats;

function isFish(animal: Fishes | Cats): animal is Fishes {
  return (animal as Fishes).doSomethingFishes !== undefined;
}

function doSomething(someAnimal: Animals): void;

function doSomething(someAnimal: Animals): void {
    if (isFish(someAnimal)) {
        doSomethingFishes(someAnimal as Fishes);
    }

    doSomethingCats(someAnimal as Cats);
}