LoginSignup
0
0

More than 1 year has passed since last update.

判別可能 Union型 (discriminated union)

Posted at

一息にいいますと、絞り込めるようになるってことです。

interface Interface01 {
  type: "Interface01";
  Interface01: number;
}

interface Interface02 {
  type: "Interface02";
  Interface02: number;
}

type InterfaceUnion = Interface01 | Interface02;

function interfaceSearch(arg: InterfaceUnion) {
  let typeNumber: number;
  switch (arg.type) {
    case "Interface01":
      typeNumber = arg.Interface01;
      break;
    case "Interface02":
      typeNumber = arg.Interface02;
  }
  console.log("Interface01: " + typeNumber);
}

interfaceSearch({ type: "Interface01", Interface01: 10 });

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0