Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

例如:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

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

function xxoo(type: XXOOType) {}

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

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

type XXOOType = $Values<typeof xxoo_type>;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...