To adapt a code quality guideline similar to Python's PEP8 for TypeScript projects, you'll want to establish a set of coding standards, linting tools, and formatting tools appropriate for TypeScript. Common tools for enforcing code quality in TypeScript projects include ESLint for linting, Prettier for formatting, and optionally, pre-commit hooks to automate checks before commits.
Here’s how you could set up and describe these practices for a TypeScript project:
For our TypeScript projects, we adhere to best practices and coding standards enforced through ESLint and formatted with Prettier. We also utilize pre-commit hooks to ensure code quality and consistency before commits are made.
ESLint is a popular linting tool for JavaScript and TypeScript that helps identify problematic patterns or code that doesn’t adhere to certain style guidelines.
-
Install ESLint and TypeScript ESLint Plugin:
npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
-
Create an ESLint configuration file: You can create a
.eslintrc.json
file in the root of your project with rules tailored to your needs. Here is a basic example:{ "parser": "@typescript-eslint/parser", "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], "parserOptions": { "ecmaVersion": 2020, "sourceType": "module" }, "rules": { // Define your own rules, or override defaults here } }
Prettier is an opinionated code formatter that supports many languages and integrates with most editors. It can be run from the command line or included as part of your build process.
-
Install Prettier:
npm install --save-dev prettier
-
Create a Prettier configuration file: You can set up a
.prettierrc
file to define formats. Here's a simple example:{ "semi": true, "singleQuote": true }
Pre-commit hooks can be used to run ESLint and Prettier before each commit, ensuring that only code that meets quality standards is committed.
-
Install pre-commit:
npm install --save-dev pre-commit
-
Update package.json: Add a
pre-commit
section in yourpackage.json
to define the hooks."pre-commit": [ "lint", "format" ], "scripts": { "lint": "eslint 'src/**/*.ts'", "format": "prettier --write 'src/**/*.ts'" }
-
Install husky (optional but recommended for better hook management):
npx husky-init && npm install npx husky add .husky/pre-commit "npm run lint && npm run format"
- For ESLint: ESLint User Guide
- For Prettier: Prettier Documentation
- For pre-commit hooks and husky: pre-commit, Husky
This setup ensures that your TypeScript code is linted for potential errors and is consistently formatted according to the project's style guidelines, much like how Python projects might use PEP8, flake8, and pre-commit hooks.
TODO