unionType

 avatar
unknown
typescript
3 years ago
1.0 kB
7
Indexable
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);
}




Editor is loading...