2021年4月29日星期四

Why does Flow assume this function returns a RegExp?

I have the following code: a function which returns an option from an array. The options could be strings or regexes, and the return value will follow suit.

const pick = (options: Array<string | RegExp>): string | RegExp =>    options[Math.floor(Math.random() * options.length)];    const myOptions = ['hello', 'goodbye']  const randomId: string = pick(myOptions);  

This raises this error:

Cannot assign pick(...) to randomId because RegExp [1] is incompatible with string [2].Flow(incompatible-type)

Why is this?

https://stackoverflow.com/questions/67326795/why-does-flow-assume-this-function-returns-a-regexp April 30, 2021 at 08:42AM

没有评论:

发表评论