TSLint 不适用于 CRA 和 TypeScript

TSLint not working with CRA and TypeScript

我花了好几个小时试图为使用 create-react-app 创建的 TypeScript 项目启用 linting。

问题是上面建议的实现没有向新创建的项目添加任何 linting。

到目前为止我已经尝试过:

  {
    "rules": {
      "no-debugger": false,
      "no-console": false,
      "interface-name": false
    },
    "linterOptions": {
      "exclude": [
        "config/**/*.js", "node_modules/**/*.ts", "coverage/lcov-report/*.js"
      ]
    },
    "extends": [
      "tslint:recommended", 
      "tslint-react", 
      "tslint-config-prettier"
    ]
  }
 "scripts": { 
     "lint": "tslint -c tslint.json src/**/*.{ts,tsx} --fix --format verbose"
 }

然后我尝试了 运行 yarn lint or npm run lint 但没有文件被上述任何方法检查过

这是我的 package.json 文件:

{
  "name": "myapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "^23.3.11",
    "@types/node": "^10.12.18",
    "@types/react": "^16.7.18",
    "@types/react-dom": "^16.0.11",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.2",
    "typescript": "^3.2.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "tslint -c tslint.json src/**/*.{ts,tsx} --fix --format verbose",
    "tslint-check": "tslint-config-prettier-check ./tslint.json"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "tslint": "^5.12.0",
    "tslint-config-prettier": "^1.17.0",
    "tslint-react": "^3.6.0"
  }
}

这是 npx create-react-app [project-name] --typescript

自动生成的 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": "preserve"
  },
  "include": [
    "src"
  ]
}

有什么想法吗?

我不得不经历同样的过程来弄清楚如何让 TSLint 和 Pretter 在 CRA + TypeScript 项目上工作。

我创建了这个 gist 分步说明 如何相应地设置它。

上述解决方案,简而言之,是确保您安装了适当的 VSCode 扩展并在 package.json 中安装了适当的依赖项,以便 TSLint 和 Prettier 都可以跟踪您的更改.