gpt4 book ai didi

javascript - Flow js 谓词函数

转载 作者:行者123 更新时间:2023-12-02 22:47:33 24 4
gpt4 key购买 nike

我正在尝试使用谓词函数 ( 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 提示 addressLine1cityzip 调用 someFunc() 时可能未定义。

这是一个有效的(嗯,错误的)示例:https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoYwwGU4FsCmYUArgHYDGALgJZylgKGn74AmY+AHgIZViUBPAA74AzkTgAnVOTqjKYUXnwAxMuTABeMAApurVpLGiAMtWYBGAFyLKk8wHMANGHLVBN+fdLOwAL2ohTztHAEotAD5dAG8AX1DUdEwABSNWanJuSnxUEgoaOlcAC3xyAGt8ST0DI1FTc3xrMAB+L0cXNw8Wtp8XAKDukJ9QmwAjODgYfG56AFJyEvLxaNQwMCNKYkl6ACEJqZnqw2MzSzAAMnPXdwELq-6E2MSMMBM4BwyZOQV9Y7rTxo2VpDBxaMAAcm44K+pHk1y6wO8oO04NG0NksIU-SBPWREPI0NQ1CgOgWpQqVV+tXqlg6Nz6gVCoRWayUBDUFCO1IBFjpggZQkeQA

知道我做错了什么吗?

提前致谢,丹

最佳答案

我认为本例中的问题是将返回值包装在 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)
}

在“尝试流程”中:https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoYwwGU4FsCmYUArgHYDGALgJZylgKGn74AmY+AHgIZViUBPAA74AzkTgAnVOTqjKYUXnwAxMuTABeMAApurVpLGiAMtWYBGAFyLKk8wHMANGHLVBN+fdLOwAL2ohTztHAEotAD5dAG8AX1DUdEwABSNWanJuSnxUEgoaOlcAC3xyAGt8ST0DI1FTc3xrMAB+L0cXNw8Wtp8XAKDukJ9QmwAjODgYfG56AFJyEvLxaNQwMCNKYkl6AEId-UNjM0swADJTsD3OgTOLvf7UWMSMMBM4BwyZOQUD2vrLGytIYOLRgADk3DBX1I8lc7gEgJ6IO0YNGUNkMIU-URwNBYPIUNQ1CgOgWpQqVV+RwaFg68L6gVCoRWayUBDUFGqhzqx0adMEDKECViQA

关于javascript - Flow js 谓词函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58330503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com