如何在 Flow 中声明一个函数来更改参数而不会出现 ESLint 错误

How to declare a function in Flow that changes arguments without ESLint errors

我在 CLI 脚本中同时使用了 ESLint 和 Flow,并且正在努力解决如何以 ESLint 不会抱怨的方式告诉 Flow 关于更改参数的函数。

#!/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 忽略它。

是否有适当的修复方法?

最简单的做法是显式声明 debug 的类型,因为这里的核心问题是 Flow 无法判断类型应该是什么,例如

let debug: (msg: string) => void = () => {};

type DebugFn = (msg: string) => void;
let debug: DebugFn = () => {};