0

是否可以在 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

4

1 回答 1

1

您能详细说明一下,您是在寻找数组类型还是元组类型?换句话说,["1min"] 或 ["1min", "5min", "1min"] 是否也可以接受?

如果这是可以接受的, types.array(types.union(types.literal("1min"), types.literal("5min")) 应该可以解决问题(你很接近!)。或者更简单:types.array(types .enum(["1min", "5min"])))

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

于 2018-06-11T18:46:44.587 回答