【问题标题】:Turn off VSCode Typescript Errors for .JS / .JSX files & keep them for .TS / .TSX files?关闭 .JS / .JSX 文件的 VSCode Typescript 错误并将它们保留用于 .TS / .TSX 文件?
【发布时间】:2021-05-25 03:57:28
【问题描述】:

我正在尝试使用 typescript 以及普通的 javascript。所以有些文件是 javascript 文件 (.js),而有些是打字稿文件 (.ts)。这行得通。

但是,我在 VSCode 中的 eslint 给了我 Typescript 错误和警告,即使是在纯 javascript 文件中也是如此。

我可以为 .js 文件关闭这些功能,但只要文件是 .ts / .tsx 就保持打开状态吗?

我的 eslint 和 typescript 配置只是 Vercels official typescript example

.eslintignore

**/node_modules/*
**/out/*
**/.next/*

.eslintrc.json

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    // Uncomment the following lines to enable eslint-config-prettier
    // Is not enabled right now to avoid issues with the Next.js repo
    "prettier",
    "prettier/@typescript-eslint"
  ],
  "env": {
    "es6": true,
    "browser": true,
    "jest": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "rules": {
    "react/react-in-jsx-scope": 0,
    "react/display-name": 0,
    "react/prop-types": 0,
    "@typescript-eslint/explicit-function-return-type": 0,
    "@typescript-eslint/explicit-member-accessibility": 0,
    "@typescript-eslint/indent": 0,
    "@typescript-eslint/member-delimiter-style": 0,
    "@typescript-eslint/no-explicit-any": 0,
    "@typescript-eslint/no-var-requires": 0,
    "@typescript-eslint/no-use-before-define": 0,
    "@typescript-eslint/no-unused-vars": [
      2,
      {
        "argsIgnorePattern": "^_"
      }
    ],
    "no-console": [
      2,
      {
        "allow": ["warn", "error"]
      }
    ]
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "exclude": ["node_modules", ".next", "out", "**/*.js", "**/*.jsx"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"] 
}

【问题讨论】:

  • 你看到 eslint 忽略文件了吗?
  • 还要看看你的tsconfig
  • 但我仍然想检查我的 .js / .jsx 文件,而不是通过打字稿。我想为这些文件保留其余的 linting 配置
  • 您是否尝试过从您的tsconfig.json 中的include 数组中删除"**/*.js"
  • 您能否在问题中也发布您的 .eslintignore 文件内容?

标签: javascript typescript visual-studio-code next.js eslint


【解决方案1】:

在您的 .eslintignore 文件中添加这一行

**/*.js

eslint 不会 lint 任何 *.js 文件。

【讨论】:

  • 感谢您的回答,但我不想删除我所有的 .js 文件。我的 linter 有一些我想使用的检查。我只是不想将打字稿应用于这些文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
  • 2018-02-03
  • 2017-05-08
  • 2019-07-12
  • 2020-11-07
  • 1970-01-01
  • 2020-01-30
相关资源
最近更新 更多