【问题标题】:non existing import stopped breaking build不存在的导入停止破坏构建
【发布时间】:2021-11-15 06:01:56
【问题描述】:

我有一个基于 create react-app 的项目,似乎自从更新了 yup 依赖项后,它停止了无效导入时的启动错误。

以前由 react-scripts build 和 npm start 启动的“模块文件名没有导出成员 X”的错误完全消失了,尽管我的 IDE 仍然发现这样的错误。

如果我尝试将不存在的导入用作变量,react-scripts buildnpm start 仍会引发错误,但如果我尝试将其用作类型则不会。

我尝试查看 this questionthis one 或其他类似问题,但没有一个像我的情况那样(即,给出问题的文件确实有导入,并且引入错误的提交只是在改变 yup package.json 中的依赖项,并相应地更改 package-lock.json。我还尝试将我的 @typescript-eslint/no-unused-vars 规则更新为错误,但它给了我太多错误,我不能保证它会捕获任何错误导出的不存在的类型并用作类型)

如果有帮助,这是我的tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "src",
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "noFallthroughCasesInSwitch": true
  },
  "include": [
    "src"
  ]
}

还有我的.eslintrc.json

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "tsconfigRootDir": ".",
    "project": [
      "./tsconfig.json"
    ]
  },
  "extends": [
    "react-app",
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  "plugins": [
    "filenames"
  ],
  "rules": {
    //- https://palantir.github.io/tslint/rules/no-null-undefined-union/ should be added once a typescript-eslint equivalent exists
    "@typescript-eslint/explicit-function-return-type": 0,
    "@typescript-eslint/no-explicit-any": 0,
    // as per https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md, we must disable the base rule to prefer the typescript one
    "no-unused-vars": 0,
    "filenames/match-exported": 1,
    "@typescript-eslint/prefer-as-const": 1,
    "@typescript-eslint/ban-types": 1,
    "@typescript-eslint/no-unnecessary-type-assertion": 1,
    "@typescript-eslint/strict-boolean-expressions": [
      1,
      {
        "allowNullableString": true,
        "allowNullableObject": true,
        "allowNullableBoolean": true,
        "allowNumber": false,
        "allowAny": true
        // we use lodash, better allow these any
      }
    ],
    "@typescript-eslint/no-unused-vars": 1,
    "no-return-await": 2,
    "curly": 2,
    "@typescript-eslint/no-inferrable-types": [
      2,
      {
        "ignoreParameters": true
      }
    ]
  }
}

感谢您的帮助。

【问题讨论】:

  • 我投票结束这个问题,因为作者在提出问题几秒钟后添加了对他自己问题的答案。然后他删除了自己的答案。这闻起来像是在尝试产生赞成票。
  • 我以为我有一个解决方案,然后意识到它不起作用,这就是我删除它的原因。我可以向你保证我根本不想产生赞成票,但我只是迫切希望修复一个非常烦人的错误,我试图寻找解决方案无济于事。
  • 您在之后添加了该答案,因此您事先准备了答案。意识到答案不正确只是在几分钟的时间范围内。这对我来说仍然听起来很奇怪。离开投票。
  • 我添加了答案,因为它位于您在发布问题之前看到的 SO 窗口中提出的链接问题中。它似乎有效,我认为有效,因为它确实在我的项目中产生了错误。然后我意识到我并不想要这些错误(因为错误太多,而且,即使我还没有正确测试它,它可能仍然不会得到文件中使用的导出类型)。
  • 抱歉,我没有正确地检查自己,ofc,但我真的需要帮助,我在发帖前尝试过尽职调查。

标签: javascript typescript eslint react-scripts


【解决方案1】:

所以我最终通过使用更新的版本重新安装我的依赖项来解决这个问题。在运行npm i 之前,我刚刚删除了我的node_module 和我的package-lock.json。我知道抑制 node_module 和 package-lock 应该没有任何效果,但我更愿意确定。

它让我的项目出现了各种新错误,但在很多情况下,我的错误是无效导入,所以对我有好处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2023-03-18
    相关资源
    最近更新 更多