TSLint无法与CRA和TypeScript一起使用

Nul*_*rue 8 typescript tslint visual-studio-code create-react-app prettier

我花了几个小时试图为使用create-react-app创建的TypeScript项目启用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"
    ]
  }
Run Code Online (Sandbox Code Playgroud)
  • 添加一个皮棉脚本到我的package.json
 "scripts": { 
     "lint": "tslint -c tslint.json src/**/*.{ts,tsx} --fix --format verbose"
 }
Run Code Online (Sandbox Code Playgroud)

然后,我尝试运行,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"
  }
}
Run Code Online (Sandbox Code Playgroud)

这是tsconfig.jsonnpx create-react-app [project-name] --typescript自动生成的

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

Jon*_*a33 10

我必须经历相同的过程,以弄清楚如何使TSLint和Pretter在CRA + TypeScript项目上工作。

我创建了要点,提供了有关如何进行相应设置的分步说明。

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