Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rimatla/a5a2c5dcf831c5744a656cfe81fadf52 to your computer and use it in GitHub Desktop.
Save rimatla/a5a2c5dcf831c5744a656cfe81fadf52 to your computer and use it in GitHub Desktop.
Create React App + TypeScript Linting with TSLint and Prettier setup on VSCode

Ps: The current setup was done on 01-04-19

Project Dependency Versions at the time 👇

  "react": "^16.7.0",
  "react-dom": "^16.7.0",
  "react-scripts": "2.1.3",
  "typescript": "^3.2.2"
  "tslint": "^5.12.0",
  "tslint-config-prettier": "^1.17.0",
  "tslint-plugin-prettier": "^2.0.1",
  "tslint-react": "^3.6.0"

Directions:

  1. Install Prettier and TypeScript TSLint Plugin extensions on your VSCode

  2. Edit (per your desire) the following on your VSCode settings 👇

{
  "editor.formatOnSave": false,
  "[javascript]": {
    "editor.formatOnSave": true
  },
  "[typescript]": {
    "editor.formatOnSave": true
  },
  "[typescriptreact]": {
    "editor.formatOnSave": true
  },
  "prettier.tslintIntegration": true,
  "prettier.eslintIntegration": true,
  "prettier.jsxSingleQuote": false,
  "prettier.singleQuote": true,
}
  1. npx create-react-app [project-name] --typescript

  2. cd into [project-name]

  3. Install the following dependecies to package.json 👇

yarn add --dev typescript 
yarn add @types/node @types/react @types/react-dom @types/jest
yarn add --dev tslint
yarn add --dev tslint-config-prettier
yarn add --dev tslint-plugin-prettier
yarn add --dev tslint-react
  1. Create a tslint.json file with the following config 👇
{
  "extends": [
    "tslint:recommended",
    "tslint-react",
    "tslint-config-prettier"
  ],
  "rulesDirectory": [
    "tslint-plugin-prettier"
  ],
  "rules": {
    "prettier": true,
    "interface-name": false
  }
}
  1. Create a .prettierrc file and add your desired rules i.e 👇
{
    "trailingComma": "es5",
    "printWidth": 100,
    "semi": false,
    "singleQuote": true
}
  1. Quit and restart VSCode again

Note: tslint-config-prettier is shipped with a little CLI tool to help you check if your configuration contains any rules that are in conflict with Prettier. (require tslint installed
In order to execute the CLI tool, first add a script for it to package.json:

{
  "scripts": {
    "tslint-check": "tslint-config-prettier-check ./tslint.json"
  }
}

Then run yarn tslint-check or npm run tslint-check

You may also run TS lint directlly as a script on your package.json

"scripts": { 
     "lint": "tslint -c tslint.json src/**/*.{ts,tsx} --fix --format verbose"
 }

Then run yarn lint or npm run lint

@gndplayground
Copy link

"lint": "tslint -c tslint.json src/**/*.{ts,tsx} --fix --format verbose"

Might not work. I Can see thhe errors when run npx tslint -c tslint.json src/**/*.{ts,tsx} --fix --format verbose. But npm run lint not work

"lint": "tslint -c tslint.json 'src/**/*.{ts,tsx}' --fix --format verbose",

Use this instead

@Holybasil
Copy link

thanks very much

@matyax
Copy link

matyax commented May 29, 2019

Thank you!

@crazilazi
Copy link

Thank you.

@AleksandrHovhannisyan
Copy link

Much appreciated, thank you!

@bee0060
Copy link

bee0060 commented Nov 18, 2019

Thanks.

And for me, I also need to install prettier while run tslint -c tslint.json src/**/*.{ts,tsx}, like:

yarn add -D prettier

@pwnreza
Copy link

pwnreza commented Feb 5, 2021

thank you very much

@clementgyimah
Copy link

Thank you very much.

In case anyone faces an error that says, Can't find module prettier, run:
yarn add --dev prettier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment