如何为包含默认导出的模块的命名导出声明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
相关产品推荐
相关产品推荐

