使用 typescript-eslint,VSCode 编辑器中无法显示 Typescript 错误。

11

我的问题是无法在VSCode的MacOS上使用eslint和@ typescript-eslint显示编辑器中的typescript错误。

虽然Eslint错误已经显示,但类型错误却没有显示,如此截图所示(tsx文件) example of type error not showing

这里显示了未使用变量的问题,但是调用typedFunction时的类型错误却没有显示。如果我在终端中运行tsc,则会出现错误。

Eslint扩展已安装在我的VSCode编辑器中。 这是我的.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]
  }
}

我尝试删除所有的VSCode扩展,卸载VSCode,重启电脑,但是仍然没有起作用。我的VSCode版本是1.46.0。


你需要为TypeScript使用tslint。 - isAif
1
不,你可以使用eslint来处理typescript。@OP,你能否检查一下VSCode正在使用的Typescript版本,并尝试其他版本?在命令面板中选择:Typescript: Select Typescript Version。 - tHeSiD
谢谢您的回答。当我打开命令面板并输入Typescript时,没有返回结果。我使用的是MacOS,这可能是原因吗? - Elie Dutheil
但是我存储库中的版本(安装在node_modules中)是版本3.9.3,全局版本是版本3.9.5。 - Elie Dutheil
3个回答

30
在您的VSC扩展面板中,输入“@builtin”,并在“特性”部分中查找“TypeScript和JavaScript语言功能”。它很可能被禁用了。启用它并重新启动VSC。正如Matt Bierner在这里建议的那样。

1
我已经尝试了多年以TS入门,但从未跨越这一步!这次完美地解决了问题。 - dsomel21
1
谢谢!我的“TypeScript和JavaScript语言功能”已经启用,但不起作用,所以我禁用了它,重新加载并重新启用了它。效果非常好! - Aaron
这对我有用!好答案! - Travis Tubbs
谢谢!老套路的“关闭再打开”方法又解决了一个 bug。 - Beadle
谢谢你。我永远不会找到这个。 - Seth Trowbridge
2
我启用了两种typescript @builtin功能,但是在悬停时仍然没有显示错误。原来我在我的settings.json中有"typescript.validate.enable": false,这就是罪魁祸首。删除那一行后,它正常工作了... - Hazy

0

-2
你需要在项目中添加一个tsconfig.json文件,这样它就可以工作了。就像这样:
{
  "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
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接