我在一个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 happy.Right,现在我告诉ESLint忽略它。
有合适的解决方法吗?
发布于 2019-11-06 09:06:57
最简单的事情是显式声明debug的类型,因为这里的核心问题是流不能告诉应该是什么类型,例如
let debug: (msg: string) => void = () => {};或
type DebugFn = (msg: string) => void;
let debug: DebugFn = () => {};https://stackoverflow.com/questions/58701687
复制相似问题