简体   繁体   English

使用 typescript-eslint 的 VSCode 编辑器中未显示 Typescript 错误

[英]Typescript errors not showing in VSCode editor using typescript-eslint

My issue is that I cannot display typescript errors in editor using eslint and @typescript-eslint (on VSCode for MacOs).我的问题是我无法使用 eslint 和 @typescript-eslint(在 MacOs 的 VSCode 上)在编辑器中显示 typescript 错误。

Eslint errors are showing but not type errors as shown on this screenshot (tsx file)显示 Eslint 错误,但未显示此屏幕截图(tsx 文件)中显示的类型错误未显示类型错误的示例

Here the issue about unused variable is displayed but the type error when calling typedFunction is not.这里显示了有关未使用变量的问题,但没有显示调用typedFunction时的类型错误。 If I run tsc in my terminal the error will raise.如果我在终端中运行tsc ,则会引发错误。

Eslint extension is installed my VSCode editor. Eslint 扩展安装在我的 VSCode 编辑器中。 Here is my.eslintrc:这是我的.eslintrc:

{
  "parser": "@typescript-eslint/parser",
  "extends": [
    "plugin:@typescript-eslint/recommended",
    "react-app",
    "prettier",
    "prettier/@typescript-eslint",
    "prettier/react"
  ],
  "plugins": [
    "import",
    "prefer-object-spread",
    "prettier",
    "react",
    "@typescript-eslint"
  ],
  "env": {
    "browser": true,
    "es6": true,
    "node": true
  },
  "globals": {
    "alert": true,
    "document": true,
    "localStorage": true,
    "navigator": true,
    "window": true,
    "HTMLElement": true
  },
  "rules": {
    "prettier/prettier": "error",
    "import/extensions": 0,
    "import/no-extraneous-dependencies": [
      "error",
      {
        "devDependencies": true,
        "optionalDependencies": false,
        "peerDependencies": false
      }
    ],
    "import/no-unresolved": 0,
    "import/prefer-default-export": 0,
    "prefer-object-spread/prefer-object-spread": 2,
    "react/destructuring-assignment": 0,
    "react/jsx-filename-extension": [1, { "extensions": [".ts", ".tsx"] }],
    "react/no-array-index-key": 2,
    "react/prefer-stateless-function": 0,
    "react/prop-types": 0,
    "react/require-default-props": 0,
    "@typescript-eslint/explicit-function-return-type": 0,
    "@typescript-eslint/explicit-member-accessibility": 0,
    "@typescript-eslint/camelcase": 0,
    "@typescript-eslint/interface-name-prefix": 0,
    "complexity": ["error", 8],
    "max-lines": ["error", 200],
    "max-depth": ["error", 3],
    "max-params": ["error", 4]
  }
}

I tried removing all VSCode extensions, uninstalling VSCode, reboot my computer but nothing worked.我尝试删除所有 VSCode 扩展,卸载 VSCode,重新启动计算机,但没有任何效果。 My VSCode version is 1.46.0我的 VSCode 版本是 1.46.0

In your VSC's Extensions panel, type "@builtin" and look for "TypeScript and JavaScript Language Features" in the "Features" section.在 VSC 的扩展面板中,键入“@builtin”并在“功能”部分中查找“TypeScript 和 JavaScript 语言功能”。 It is most probably disabled.它很可能已被禁用。 Enable it and restart your VSC.启用它并重新启动您的 VSC。 As suggested by Matt Bierner from here .正如 Matt Bierner 从这里建议的那样。

VSCode does not have an in-built typescript linter. VSCode 没有内置的 typescript linter。 https://code.visualstudio.com/docs/languages/typescript#_linters https://code.visualstudio.com/docs/languages/typescript#_linters

Install ESlint extension from the marketplace for JS and TS lint error.从市场安装 ESlint 扩展以解决 JS 和 TS lint 错误。 If it is already installed, and typescript is introduced at later stage, just reinstall ESlint extension and restart the VSCode如果已经安装了,后期引入typescript,重新安装ESlint扩展,重启VSCode即可

You need to add a tsconfig.json file to your project, it would work.您需要将tsconfig.json文件添加到您的项目中,它会起作用。 Something like this像这样的东西

{
  "exclude": ["node_modules", "node_modules/**/*"],
  "compilerOptions": {
    "baseUrl": "./",
    "lib": ["es5", "es6", "dom", "es2018.promise"],
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./build",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": false,
    "strict": true,
    "strictPropertyInitialization": false,
    "skipLibCheck": true, 
    "suppressImplicitAnyIndexErrors": true
  }
}

I finally found a solution !我终于找到了解决办法! Removing /Users/<user>/Library/Application\ Support/Code folder before uninstalling VSCode and then reinstall it.在卸载 VSCode 之前删除/Users/<user>/Library/Application\ Support/Code文件夹,然后重新安装它。 This removed the previous settings of VSCode which wasn't the case when simply deleting it from Application folder.这删除了以前的 VSCode 设置,而从应用程序文件夹中删除它时并非如此。

I don't know what was wrong but I couldn't see anything related to typescript in the interface before (like the version used next to Typescript in bottom bar) but this fixed the problem.我不知道出了什么问题,但是我之前在界面中看不到与 typescript 相关的任何内容(例如底部栏中 Typescript 旁边使用的版本),但这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM