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

问题描述 投票:0回答:1

我在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忽略它。

是否有适当的解决方法?

javascript node.js eslint flowtype
1个回答
0
投票

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

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

type DebugFn = (msg: string) => void;
let debug: DebugFn = () => {};
© www.soinside.com 2019 - 2024. All rights reserved.