You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为包含默认导出的模块的命名导出声明Flow类型?

解决Flow模块类型声明的解析错误问题

你的错误在于使用了不符合Flow语法的模块声明方式,正确的CommonJS模块类型声明需要用declare module块包裹,并使用Flow的导出声明语法来对应CommonJS的导出。

正确的.flow文件代码如下:

declare module {
  // 对应 module.exports = function1 的默认导出
  declare export default (param: ?(string | number)) => null | number;

  // 对应 exports.names 的命名导出
  declare export var names: $ReadOnlyArray<string>;

  // 对应 exports.getStrings 的命名导出
  declare export var getStrings: () => {[string]: string};
}

关键说明:

  • Flow的模块类型声明必须放在declare module块内部,不能直接在全局作用域使用declare module.function1或declare exports.xxx。
  • declare export default用于声明模块的默认导出,对应CommonJS的module.exports = ...。
  • declare export var(或const/let)用于声明命名导出,对应CommonJS的exports.xxx = ...。

内容的提问来源于stack exchange,提问作者Dhruv Tailor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:25:19