js flow 怎么指定其中可选值?

使用 flow 请问怎么让参数只能为可选值呢?

例如:

function test (x: number) {
    console.log(x);
}

怎么让 x 期望是 数字 1 或者 2

阅读 1.7k
1 个回答

enum XXOOType {XX = 1; OO = 2;};

function xxoo(type: XXOOType) {}

看了一下原来是flow,那就是这样

const xxoo_type = {XX: 1, OO: 2};

type XXOOType = $Values<typeof xxoo_type>;

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题