首页 > 解决方案 > cmd eslint 找不到 IDE 发现的 ts 错误

问题描述

我想在 cmd 中使用 eslint 来检查 ts 文件,但它无法获取我在 IDE 中获得的错误信息。

我在 eslintrc.js 中设置了@typescript-eslint/parser。当我做错时,在 cmd 中运行的 eslint 给了我一些 ts 错误。但是没有发现其他一些错误。

我有一个带有代码的 ts 文件:

interface Item {
  name: string;
  age: number;
}
const config: Array<Item> = [{
  name: 'foo',
}]

所以,我在IDE中遇到了一些错误:

Property 'age' is missing in type '{ name: string; age: number }' but required in type 'Item'.ts(2741)

没错。我需要这个错误信息。

但是当我在 cmd 中运行 eslint

eslint fileName.ts  or  eslint --ext .ts fileName.ts

cmd eslint 在此文件中不返回任何内容或其他一些警告/错误。

eslintrc 在这里


module.exports = {
  "extends": ["xxx"],
  "globals": {
    "__SERVER_ENV__": true,
  },
  "rules": {
    "react/jsx-filename-extension": 0,
    "no-console": ["warn", { allow: ["error", "warn"] }],
    "@typescript-eslint/no-unused-vars": ["error", {
      "vars": "all",
      "args": "after-used",
      "ignoreRestSiblings": true
    }],
  },

  "settings": {
    "react": {
      "createClass": "createReactClass", // Regex for Component Factory to use,
      // default to "createReactClass"
      "pragma": "React",  // Pragma to use, default to "React"
      "version": "detect", // React version. "detect" automatically picks the version you have installed.
      // You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
      // default to latest and warns if missing
      // It will default to "detect" in the future
    },
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      // use <root>/tsconfig.json
      "typescript": {
        // always try to resolve types under `<roo/>@types` directory even it doesn't contain any source code, like`@types/unist`
        "alwaysTryTypes": true,
        "directory": "./",
      },
    },
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json",
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "modules": true,
    },
  },
  "plugins": ["@typescript-eslint", "import"],
};

和 tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "lib": ["es6","dom","es2017"],
    "target": "es5",
    "jsx": "react",
    "types":["react"],
    "outDir": "/output",
    "baseUrl": ".",
    "paths": {
        "src/*": ["./src/*"]
    },
    "allowSyntheticDefaultImports": true,
    "sourceMap": true
  },
  "awesomeTypescriptLoaderOptions": {
    "useWebpackText": true,
    "useTranspileModule": true,
    "doTypeCheck": true,
    "forkChecker": true
  },
  "include": [
    ".d.ts",
    "./src/**/*"
  ]
}


我希望通过 cmd 获得整个错误信息。我应该怎么办?

标签: eslinttypescript-eslint

解决方案


eslint 似乎不会执行您的 Typescript 类型检查。我只是在本地运行我的构建命令,tsc以便收到“类型中缺少属性”错误消息的通知,并在构建失败时进行修复。

这非常不方便,因为每次构建尝试我只会收到一个错误警报。如果有人有更好的解决方案,我将不胜感激。


推荐阅读