TSLint无法与CRA和TypeScript一起使用

时间:2019-01-04 07:17:33

标签: typescript visual-studio-code create-react-app tslint prettier

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

问题在于上述建议的实现方式不会对新创建的项目添加任何棉绒。

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

  • 安装TypeScript TSLint Plugin作为我的VSCode的扩展
  • 使用以下配置在我的项目中创建tslint.json文件:
  
  {
    "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"
    ]
  }
  • 向我的package.json
  • 添加 lint脚本
   
 "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 [项目名称] --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"
  ]
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

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

我创建了此gist,其中包含有关如何进行相应设置的逐步说明。

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