javascript - Flow js谓词函数
问题描述
我正在尝试使用谓词函数(https://flow.org/en/docs/types/functions/#toc-predicate-functions),但它不起作用。
给定以下代码:
/* @flow */
// Some function we need exact types for
const someFunc = (addressLine1: string, city: string, zip: string) => ({})
// Predicate
function checker(addressLine1: ?string, city: ?string, zip: ?string): boolean %checks {
return Boolean(addressLine1 && city && zip)
}
// Logic
const addressLine1: ?string = 'a'
const city: ?string = 'b'
const zip: ?string = 'c'
if(checker(addressLine1, city, zip)){
someFunc(addressLine1, city, zip)
}
阅读文档后,我希望上面的代码没有任何错误,但是 Flow 抱怨addressLine1
,city
和zip
在调用someFunc()
.
任何洞察我做错了什么?
在此先感谢,丹
解决方案
我相信这种情况下的问题是将返回值包装在Boolean()
. 一旦我删除错误就解决了。
更正的代码:
/* @flow */
// Some function we need exact types for
const someFunc = (addressLine1: string, city: string, zip: string) => ({})
// Predicate
function checker(addressLine1: ?string, city: ?string, zip: ?string): boolean %checks {
return !!addressLine1 && !!city && !!zip
}
// Logic
const addressLine1: ?string = 'a'
const city: ?string = 'b'
const zip: ?string = 'c'
if(checker(addressLine1, city, zip)){
someFunc(addressLine1, city, zip)
}
推荐阅读
- chef-infra - 如何从厨师食谱访问元数据“source_url”?
- python - 为什么我的 CNN 回归网络不学习?
- visual-studio - 如何在 Azure DevOps / TFS 代码存储库中处理 C# 项目中的密码和机密
- css - Bootstrap Dropdowns 按钮不适用于 Angular 8 组件
- c# - ASP.NET Core MVC 等效于开始操作特定页面
- python - Tkinter 顺序显示/隐藏图像不起作用
- angular - Angular-Firebase Google 身份验证(错误 403:restricted_client)
- c++ - stl find函数从deque返回什么
- git - git(管道)在两次提交之间更改了文件?
- sorting - UWP TreeView 排序项目