【问题标题】:eslint-plugin-import how to add flow declared modules to exceptionseslint-plugin-import 如何将流声明的模块添加到异常中
【发布时间】: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


【解决方案1】:

我找到了解决方案。正如@logansmyth 在评论中建议的那样

您的类型应该只是将您的代码与您的数据一起传递

我遇到的问题是 webpack 别名。 Flow 指出了有关未安装模块的错误。但是我 found out 我可以在 .flowconfig 中使用映射器,例如:

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

与 webpack 别名一起,这使得 eslint-plugin-import 和 flow 以及正确的类型检查都很开心。现在我将类型与common 组件一起导入,不会弄乱流类型目录。

【讨论】:

猜你喜欢
  • 2020-02-12
  • 1970-01-01
  • 2021-03-31
  • 2020-07-08
  • 1970-01-01
  • 2019-06-28
  • 2015-12-17
  • 2023-03-26
  • 2020-07-16
相关资源
最近更新 更多