javascript - 如何在 Flow 中声明更改参数而不会出现 ESLint 错误的函数

标签 javascript node.js eslint flowtype

我在 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/

相关文章:

node.js - 继承已实现类型 graphql 中的接口(interface)字段

javascript - 如何在 eslint/tslint 中配置忽略单行语句?

react-native - .eslintrc.js 不适用于规则 "off"

javascript - 我如何合并 datauris 类型 : image/png to mpg video file in javascript

javascript - 使用 Javascript 发布表单

node.js - 在文件夹中创建 Google Sheets 文档(无需移动和删除旧文档)

node.js - 如何使用 MongoDB Node 驱动程序通过 id 查找?

javascript - 当多次调用时,Angular 服务会覆盖自身

javascript - 尝试使用过滤器设置具有唯一值的数组失败

javascript - ESLINT 无限循环