【发布时间】:2018-02-08 05:15:10
【问题描述】:
我在流类型目录中有一个文件,其中包含一些常见的类型声明,例如:
common-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-plugin-import 错误以及无法解析模块“common-types”的路径
我该如何处理?
【问题讨论】:
-
common-types实际上是一个 npm 模块吗?似乎将其设为本地文件会更容易,如果您实际上并未声明真正的 npm 模块的类型,请执行./common-types。 -
它不是一个模块。我想避免像
../../../../../common-types这样的长时间导入。如果我制作 webpack 别名,那么流程再次显示有关未找到模块的错误。 -
通常,如果您必须这样做,则表明您的文件结构没有得到很好的考虑。您的类型应该只是将您的代码与您的数据一起传递,因此如果您导出一个返回类型的函数,请也从该模块重新导出该类型。如果你导入一个返回类型的函数,你很可能可以从同一个模块导入类型。
-
当我使用 webpack 别名时出现问题。为了使流在我从
commons导入组件时不会引发错误,我必须在declare module 'common' { declare module .exports: any; }的流类型中忽略它,但是如果我尝试从那里导出类型,我不会收到关于导出类型的错误不存在,因为它被忽略了。 -
@loganfsmyth 感谢您的建议。在几乎放弃之后,我尝试了另一种方法,现在可以了。
标签: eslint flowtype eslint-config-airbnb