我在 CLI 脚本中同时使用了 ESLint 和 Flow,并且正在努力解决如何告诉 Flow 一个以 ESLint 不会提示的方式更改参数的函数。
#!/usr/bin/env node
// I define a dummy function that gets redefined later
let debug = () => {}
// let debug = (msg?:string) => {}" // ESLint complains about unused vars
// Main function - in here debug gets defined based on some new data
const foo = () => {
...
debug = (msg: string) => console.log(msg)
}
// Outside of the main func I still need to call it in a couple of places.
debug('go') // Flow: Cannot call `debug` because no arguments are expected by function
const handleRejection = async err => {
debug('error')
}
process.on('unhandledRejection', handleRejection)
// Run script (CLI)
foo()
代码工作正常,但我想知道是否有更好的方法可以让 ESLint 和 Flow 满意。现在我告诉 ESLint 忽略它。
有正确的解决办法吗?
最佳答案
最简单的方法是显式声明 debug
的类型,因为这里的核心问题是 Flow 无法判断类型应该是什么,例如
let debug: (msg: string) => void = () => {};
或
type DebugFn = (msg: string) => void;
let debug: DebugFn = () => {};
关于javascript - 如何在 Flow 中声明更改参数而不会出现 ESLint 错误的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58701687/