Home > Blockchain >  How to create function returns generic type, which extends from union type
How to create function returns generic type, which extends from union type

Time:06-12

Here is my example and I don't know how to implement the function "MakeCoffee". Please help. thanks.

interface Latte {
  ouncesEspresso: number;
  milkToEspresso: 4;
};
 
interface Cappuccino = {
  ouncesEspresso: number;
  milkToEspresso: 2;
};

interface IceLatte extends Latte {
  icePortion: "light" | "normal" | "extra";
}

type Coffee = Latte | Cappuccino;

function MakeCoffee<T extends Coffee>(): T {
  // I don't know how to figure out which type is used in that union type
  // is this possible? if not, how to change the design
}

CodePudding user response:

From my experience, using generic parameter which is not binded to any function argument is a bad practice. There cases when you need to do it, but this is not about your case.

If you wan just to make builder function, try this:

interface Latte {
  ouncesEspresso: number;
  milkToEspresso: 4;
};

interface Cappuccino {
  ouncesEspresso: number;
  milkToEspresso: 2;
};

const coffeeKind = <
  MilkCount extends 4 | 2
>(coffee: Coffee, milkCount: MilkCount):
  coffee is Extract<Coffee, { milkToEspresso: MilkCount }> =>
  coffee.milkToEspresso === milkCount

interface IceLatte extends Latte {
  icePortion: "light" | "normal" | "extra";
}

type Coffee = Latte | Cappuccino;

function makeCoffee(milkToEspresso: 4, ouncesEspresso: number): Latte
function makeCoffee(milkToEspresso: 2, ouncesEspresso: number): Cappuccino
function makeCoffee(milkToEspresso: Coffee['milkToEspresso'], ouncesEspresso: number) {
  return {
    milkToEspresso,
    ouncesEspresso,
  }
}

const cappuccino = makeCoffee(2, 4) // Cappuccino
const latte = makeCoffee(4, 4) // Latte

enter link description here

See function-overloads

  • Related