eslint-插件导入如何增加流量声明模块异常

问题描述:

我在流类型的目录中一些常见的类型声明就像一个文件:eslint-插件导入如何增加流量声明模块异常

共types.js

// @flow 

declare module 'common-types' { 
    declare export type RequestState = { 
    setLoading:() => void, 
    setFinished:() => void, 
    setError: (error: AxiosFailure) => void, 
    reset:() => void, 
    status: LoadingStatus, 
    error: AxiosFailure | void, 
    }; 

    declare export type LoadingStatus = '' | 'loading' | 'finished'; 

    declare export type ErrorObject = { [key: string]: string | string[] | Error }; 

    declare export type AxiosFailure = { 
    status: number, 
    data: ErrorObject, 
    } 

} 

现在,我将其导入这样的文件:

import type { RequestState } from 'common-types'; 

,但我得到eslint-插件导入错误约丢失的文件扩展名以及无法解决路径到模块的'常见类型'

我该如何处理它?

+0

'common-types'实际上是一个npm模块吗?似乎将它变成本地文件会更容易,如果你实际上没有声明真正的npm模块的类型,那么执行'./ common-types'。 – loganfsmyth

+0

它不是一个模块。我想避免像'../../../../../ common-types'那样长时间导入。如果我制作webpack别名,那么流再次显示关于未找到模块的错误。 – Tomasz

+0

通常,如果你不得不这样做,这表明你的文件结构不是很好的考虑因素。你的类型应该只传递你的代码和你的数据,所以如果你导出一个返回类型的函数,那么也要从那个模块中重新导出类型。如果你导入一个返回类型的函数,你可以从同一个模块导入类型。 – loganfsmyth

我找到了解决方案。作为@logansmyth在评论

建议你的类型应该只是通过你的代码与您的数据

我的问题是用的WebPack别名一起。 Flow指出我有关未安装模块的错误。 Hovewer我found out,我可以使用映射器在.flowconfig像:用的WebPack别名一起

module.name_mapper='^common' ->'<PROJECT_ROOT>/src/common' 

,这使得eslint-插件导入和流量快乐以及正确类型检查。现在我导入类型与common组件,没有搞流式目录。