📜  typescript 切换对象类型 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:25.083000             🧑  作者: Mango

代码示例1
interface Action {}

class SpecificAction implements Action {
  kind: "specific";
  payload?: any;
}

class ToggleAction implements Action {
  kind: "toggle";
  toggle: boolean;
}

let action: SpecificAction | ToggleAction;
switch (action.kind) {
  case "specific":
    console.log(action.payload) // it works 
    break;
  case "toggle":
    console.log(action.toggle) // it works 
    break;        
}