mobx-state-tree文字数组

时间:2018-06-09 21:02:36

标签: typescript state mobx-state-tree

是否可以在MST中创建文字数组类型?它应该等同于

type Interval = ['1min', '5min'];

这是一个抛出错误的代码示例

type Interval = '1min' | '5min';

export interface AppStore {
  config: {
    intervals: ['1min', '5min']
  },
  search: {
    symbol: string;
    interval: string;
    dataTypes: string[];
  }
}

const appStore = types
  .model<AppStore>('appStore', {
    config: types.model({
      intervals: types.array(
        types.literal<Interval>('1min'),
        types.literal<Interval>('5min')
      ),
    }),
    search: types.model({
      dataTypes: types.array( types.string ),
      interval: types.union(
        types.literal<Interval>('1min'),
        types.literal<Interval>('5min')
      ),
      symbol: types.string,
    }),
  })

types.array()期望有一个参数,所以它甚至可能吗? MST文档https://github.com/mobxjs/mobx-state-tree#types-overview

没有运气

1 个答案:

答案 0 :(得分:1)

你能详细说明一下,你在寻找数组类型还是元组类型?换句话说,是[&#34; 1min&#34;],还是[&#34; 1min&#34;,&#34; 5min&#34;,&#34; 1min&#34;]也可以接受?

如果这是可以接受的,则types.array(types.union(types.literal(&#34; 1min&#34;),types.literal(&#34; 5min&#34;))应该做的伎俩(你很接近!)。或者更简单:types.array(types.enum([&#34; 1min&#34;,&#34; 5min&#34;])))

如果您正在寻找尚未实现的元组类型:请参阅https://github.com/mobxjs/mobx-state-tree/issues/805