此代码通过流检查:
/* @flow */
function test (list: ?Array<string>): Promise<number> {
if(list !== null && list !== undefined) {
return Promise.resolve(list.length)
} else {
return Promise.resolve(0)
}
}
console.log(test(null))
Run Code Online (Sandbox Code Playgroud)
以下获取空检查错误
/* @flow */
function test (list: ?Array<string>): Promise<number> {
if(list !== null && list !== undefined) {
return Promise.resolve().then(() => list.length)
} else {
return Promise.resolve(0)
}
}
console.log(test(null))
Run Code Online (Sandbox Code Playgroud)
错误:
property `length`. Property cannot be accessed on possibly null value
Run Code Online (Sandbox Code Playgroud)
显然列表不能null这样,必须有一些代码结构,使流程无法识别这一点.
我想了解我遇到的限制以及我如何解决它.谢谢!
flowtype ×1