首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建可扩展枚举以用于可扩展接口

创建可扩展枚举以用于可扩展接口
EN

Stack Overflow用户
提问于 2017-09-03 16:24:42
回答 1查看 2.1K关注 0票数 0

上下文:,我正在尝试开发一种模式,用于使用TypeState库在类型记录中创建可扩展的状态机。TypeState为类型记录提供了一个类型文件状态机,虽然它不是问题的核心,但它有助于说明我的目标。

问题:遇到了一些问题,创建了在类型记录中扩展enum并在interfaceclass声明中实现它们的可伸缩模式。

目标:下面的psuedocode说明了我希望我的模式是什么样子。

1)定义基本enum States

2)扩展具有附加状态的enum States,从而导致enum ExtendedStates

2)使用ParentInterface和类型化状态机定义States

3)通过ParentInterface扩展ChildInterface,用ExtendedStates覆盖States

4)在ParentInterface中实现class Parent

5)扩展class Parentclass Child中实现ChildInterface

6)能够从任何一个类调用broadcastState()并获取当前状态。

我在其他语言中使用过这种模式,我希望能帮助理解打字本的局限性和任何可以实现相同目标的替代模式。

代码语言:javascript
复制
import {TypeState} from "typestate";

enum States {
  InitialState
}

// extends is not available on enum, looking for alternative
enum ExtendedStates extends States {
  AdditionalState
}

/////////////////////////////////////////
// this works fine
interface ParentInterface {
  fsm: TypeState.FiniteStateMachine<States>;
  states: typeof States;
  message: string;
}

// incorrectly extends ParentInterface, types of fsm/states are incompatible
interface ChildInterface extends ParentInterface {
  fsm: TypeState.FiniteStateMachine<ExtendedStates>;
  states: typeof ExtendedStates;
}

/////////////////////////////////////////

class Parent implements ParentInterface {
  public fsm: TypeState.FiniteStateMachine<States>;
  public states: typeof States;
  public message: string = "The current state is: ";

  constructor(state: States | undefined) {
    state = state ? state : this.states.InitialState;
    this.fsm = new TypeState.FiniteStateMachine(state);
    this.broadcastCurrentState();
  }

  public broadcastCurrentState(): void {
    console.log(this.message + this.fsm.currentState);
  }
}

class Child extends Parent implements ChildInterface {
  public fsm: TypeState.FiniteStateMachine<ExtendedStates>;
  public states: typeof ExtendedStates;

  constructor(state: ExtendedStates | undefined) {
    state = state ? state : this.states.InitialState;
    this.fsm = new TypeState.FiniteStateMachine(ExtendedStates);
    this.broadcastCurrentState();
  }
}

离我最近

代码语言:javascript
复制
import {TypeState} from "typestate";

enum States {
  InitialState
}

enum ExtendedStates {
  InitialState,
  ExtendedState
}

class Parent {
  public fsm: TypeState.FiniteStateMachine<States>;
  public states: typeof States;
  public message: string = "The current state is: ";

  // T is declared but never used
  constructor(state: <T> | undefined) {
    state = state ? state : this.states.InitialState;
    // cannot find name T
    this.fsm = new TypeState.FiniteStateMachine<T>(state);
    this.broadcastCurrentState();
  }

  public broadcastCurrentState(): void {
    console.log(this.message + this.fsm.currentState);
  }
}

// types of fsm are incompatible
class Child extends Parent {
  public fsm: TypeState.FiniteStateMachine<ExtendedStates>;
  public states: typeof ExtendedStates;

  constructor(state: ExtendedStates | undefined) {
    // Param not assignable to type <T>
    super(state);
  }
}

这种尝试接近预期的结果,但没有编译,导致enum中大量的代码重复。它也失去了接口,这不是一个要求,但确实提供了一个良好的安全网。

我很想听听你们都要说什么。我觉得这是一个强大的模式,为了实现它,我错过了一些简单的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-05 19:16:22

它不编译的原因之一是因为Child不是Parent的一个适当的子类型。Liskov代换原理说您应该能够使用Child对象作为Parent对象。如果我问一个Parent对象它的状态机在哪个状态,并且它告诉我ExtendedState,那么我就得到了一个坏的Parent,对吗?因此,Child是一个坏的Parent,这是TypeScript警告您的内容。

也许最好忘记拥有超类/子类关系,而只是拥有一个泛型类:

代码语言:javascript
复制
class Generic<T extends States> {
  public fsm: TypeState.FiniteStateMachine<T>;
  public states: T;
  public message: string = "The current state is: ";

  // T[keyof T] means the values of T, in this case InitialState, etc    
  constructor(state: T[keyof T] | undefined) {
    state = state ? state : this.states.InitialState;
    // cannot find name T
    this.fsm = new TypeState.FiniteStateMachine<T>(state);
    this.broadcastCurrentState();
  }

  public broadcastCurrentState(): void {
    console.log(this.message + this.fsm.currentState);
  }
}

现在,如果States是正确的对象,那就可以了,但正如您所注意到的,enum的功能并不完全适合以这种方式使用:您无法获得任何东西来扩展它们。因此,与其使用enum,不如使用一个模仿它的对象:

代码语言:javascript
复制
// make our own enum
type Enum<T extends string> = {[K in T]: K};

// create an enum from given values
function makeEnum<T extends string>(...vals: T[]): Enum<T> {
  const ret = {} as Enum<T>;
  vals.forEach(k => ret[k] = k)
  return ret;
}

// take an existing enum and extend it with more values
function extendEnum<T extends string, U extends string>(
  firstEnum: Enum<T>, ...vals: U[]): Enum<T | U> {
    return Object.assign(makeEnum(...vals), firstEnum) as any;  
}

在本例中,Enum<>是一个具有指定字符串键的对象,其值与键相同(这与值为数字的常规enum略有不同。如果你真的想要的数字,也许可以安排,但这将是更烦人的实现。我从未使用过TypeState库,所以我不知道它是否关心这些值是数字还是字符串。)现在您可以像这样创建StatesExtendedStates

代码语言:javascript
复制
const States = makeEnum('InitialState'); 
type States = typeof States; 
// States is { InitialState: 'InitialState' };

const ExtendedStates = extendEnum(States, 'ExtendedState');
type ExtendedStates = typeof ExtendedStates;
// ExtendedStates is { InitialState: 'InitialState', ExtendedState: 'ExtendedState' };

并创建这样的对象:

代码语言:javascript
复制
const parentThing = new Generic<States>(States.InitialState);
const childThing = new Generic<ExtendedStates>(ExtendedStates.InitialState);

希望这会有所帮助;祝你好运!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46025487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档