【问题标题】:Configure ESLint to parse .ts and .tsx as Typescript and .js and .jsx as Ecmascript配置 ESLint 以将 .ts 和 .tsx 解析为 Typescript,将 .js 和 .jsx 解析为 Ecmascript
【发布时间】:2020-11-07 05:02:20
【问题描述】:

我已经安装了 typescript 以在我的 Create React App 项目中使用。我想逐步将 ES 文件重构为 TS。

但 linter 现在也将 .js 和 .jsx 文件解析为 Typescript。

/project/file.js
  19:35  warning  Missing return type on function  @typescript-eslint/explicit-function-return-type
  20:35  warning  Missing return type on function  @typescript-eslint/explicit-function-return-type

是否可以将 .js 和 .jsx 文件解析为 Ecmascript,将 .ts 和 .tsx 文件解析为 Typescript?

我的配置是:

./eslintrc

{
  "extends": [
    "airbnb",
    "prettier",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint"
  ],
  "parser": "@typescript-eslint/parser",
  "rules": {
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".tsx", ".ts"] }],
  }
}

./tsconfig.json

{
  "compilerOptions": {
    "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"
  },
  "include": [
    "src"
  ]
}

用来运行的命令:

{
  "scripts": {
    "lint": "node_modules/.bin/eslint --ext=.jsx,.js,.tsx,.ts  ."
  }
}

【问题讨论】:

    标签: javascript reactjs typescript eslint


    【解决方案1】:

    啊,你应该使用 eslint.rc as described in this post 中的 overrides 属性。

    "overrides": [
        {
          "files": ["*.ts", "*.tsx"],
          "extends": [
            "plugin:@typescript-eslint/eslint-recommended",
            "plugin:@typescript-eslint/recommended",
            "prettier/@typescript-eslint"
          ],
          "parser": "@typescript-eslint/parser",
          "plugins": ["@typescript-eslint"]
        }
      ]
    

    然后我注意到包含 TS 的 ES 文件没有找到 .ts 文件:

    /project/file.js
      3:26  error  Unable to resolve path to module './AnotherComonent' import/no-unresolved
      3:26  error  Missing file extension for "./AnotherComonent"       import/extensions
    

    可以通过 (source) 将其添加到 .eslintrc(推荐)来解决:

    {
      "extends": ["plugin:import/typescript"],
      "rules": {
        "import/extensions": [
          "error",
          "always",
          {
            "js": "never",
            "jsx": "never",
            "ts": "never",
            "tsx": "never"
          }
        ],
    }
    
    

    或者(sourcesource):

    {
      "settings": {
        "import/resolver": {
          "node": {
            "extensions": [".js", ".jsx", ".ts", ".tsx"]
          }
        }
      },
      "rules": {
        "import/extensions": [
          "error",
          "ignorePackages",
          {
            "js": "never",
            "jsx": "never",
            "ts": "never",
            "tsx": "never"
          }
        ]
      }
    }
    

    换句话说,手动为文件添加解析器。

    并忽略 im/extensions 错误。不理想,但在撰写本文时似乎是唯一的方法。

    【讨论】:

    • 这似乎有错误。您谈论在代码上添加代码。另外,在 Jason 推荐的末尾还有一个额外的逗号。
    • 随时提出修改建议。添加“代码上的代码”正是该博文的重点。有时您无法在整个代码库中交换语言设置。非常好,您很挑剔,但添加有关您为什么会采取不同做法的信息。 (似乎*,因为主语是复数,而且是 JSON,而不是 JASON)
    • 我希望你能做出很好的澄清,而你做到了。我不是这样的批评家。你所做的很好。我只是认为真正新的人仍然难以实施您的建议。那真的不是你的错。总的来说很难。很多时候,linting 比一开始就写代码要难。
    猜你喜欢
    • 2020-11-18
    • 2020-11-10
    • 2021-11-01
    • 2017-08-17
    • 1970-01-01
    • 2021-04-04
    • 2016-08-05
    • 2021-06-05
    • 2020-11-15
    相关资源
    最近更新 更多