我在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 = () => {};