Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Testing and Tooling

Tooling: TypeScript, ESLint and Prettier

Set up the quality tooling around a Vite project: tsc type checks, ESLint flat config, Prettier, Git hooks, npm scripts, editor settings and a CI pipeline.

Vite is deliberately fast and narrow: it transforms and bundles. It does not lint your code, check your types or enforce formatting. These jobs belong to other tools, and a healthy project wires them together with a few npm scripts. This lesson shows the standard setup, why each tool is separate, and how to run them in an editor, a Git hook and CI.

Configuration files are real and read-only. The stand-ins model the "run all checks, report one result" idea, which is what a CI script does.

Who does what

Tool Question it answers Runs in Vite?
esbuild (inside Vite) "Can I turn this TS/JSX into JS?" Yes: strips types
tsc --noEmit "Are the types consistent?" No: run separately
ESLint "Does this code follow rules and avoid bug patterns?" No
Prettier "Is this formatted consistently?" No
Vitest "Does behavior match expectations?" Shares the config

Because esbuild never type-checks, a build can succeed with type errors. That is why a type-check script is mandatory in a TypeScript project.

TypeScript checks

JSON
{
  "scripts": {
    "typecheck": "tsc --noEmit",
    "typecheck:watch": "tsc --noEmit --watch"
  }
}

For Vue single-file components use vue-tsc --noEmit. Many templates split config into tsconfig.app.json and tsconfig.node.json (for vite.config.ts, which runs in Node); in that layout use tsc -b to check both. If you want errors in the browser overlay during development, add vite-plugin-checker.

ESLint with a flat config

ESLint 9 uses a single eslint.config.js file:

JavaScript
// eslint.config.js (real ESLint, read-only)
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';

export default tseslint.config(
  { ignores: ['dist', 'coverage'] },
  {
    files: ['**/*.{ts,tsx}'],
    extends: [js.configs.recommended, ...tseslint.configs.recommended],
    languageOptions: { globals: globals.browser },
    plugins: { 'react-hooks': reactHooks, 'react-refresh': reactRefresh },
    rules: {
      ...reactHooks.configs.recommended.rules,
      'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
      'no-console': ['warn', { allow: ['warn', 'error'] }],
    },
  },
);

The react-refresh rule matters in Vite projects: Fast Refresh only keeps state when a file exports only components, and the rule warns when a file mixes components with other exports.

JSON
{ "scripts": { "lint": "eslint .", "lint:fix": "eslint . --fix" } }

Prettier

Prettier formats; ESLint finds problems. Do not make ESLint enforce formatting. If both are used, add eslint-config-prettier to switch off ESLint rules that conflict.

JSON
// .prettierrc
{ "singleQuote": true, "semi": true, "printWidth": 100, "trailingComma": "all" }
JSON
{ "scripts": { "format": "prettier --write .", "format:check": "prettier --check ." } }

Add a .prettierignore with dist and coverage. Agree on one config in the team and let the tool end formatting arguments.

Git hooks

Catch problems before they reach the repository by running checks on staged files only, using husky and lint-staged:

JSON
{
  "lint-staged": {
    "*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"],
    "*.{css,md,json}": ["prettier --write"]
  }
}

A pre-commit hook runs npx lint-staged. Keep hooks fast (seconds); put slow checks such as the full test suite in CI.

One script to run everything

JSON
{
  "scripts": {
    "check": "npm run typecheck && npm run lint && npm run format:check && npm run test:run"
  }
}
JS Stand-in: run all checks, report once

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A real CI job does the same: any non-zero exit code fails the pipeline.

A CI workflow

YAML
# .github/workflows/ci.yml (real GitHub Actions, read-only)
name: CI
on: [push, pull_request]
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm }
      - run: npm ci
      - run: npm run check
      - run: npm run build

npm ci installs exactly what the lockfile says. Running build last proves the production build still works. Cache node_modules/.vite only if builds are slow; it can hide dependency problems.

Editor integration

Install the ESLint and Prettier extensions in your editor, enable format-on-save, and commit shared settings in .vscode/settings.json or an .editorconfig so everyone sees the same result:

Text
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true

Env files and types

Variables exposed to client code must start with VITE_; everything else stays on the server and is not exposed. Never put secrets in VITE_ variables, because they end up in the public bundle. Commit .env.example, ignore .env.local, and type the variables in src/vite-env.d.ts as shown in Framework Templates and Setup.

JS Stand-in: which variables reach the client?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a Vite build succeed even with TypeScript type errors?

    Type checking is separate: run tsc --noEmit (or vue-tsc) in scripts and CI.
  2. 2. What is the division of labor between ESLint and Prettier?

    Keep them separate; eslint-config-prettier disables ESLint rules that conflict with formatting.
  3. 3. Which environment variables does Vite expose to client code by default?

    The VITE_ prefix is a safety filter. Anything with that prefix ends up in the public bundle.
  4. 4. What should a pre-commit hook run?

    Hooks must be quick; leave long checks for CI.

Summary

  • Vite does not type-check, lint or format: add tsc --noEmit, ESLint and Prettier as scripts.
  • Use ESLint's flat config and the react-refresh rule for Fast Refresh safety.
  • lint-staged with a pre-commit hook gives fast feedback; CI runs the full check and build.
  • Only VITE_ variables reach the browser: never place secrets there.
  • Share editor settings through .editorconfig and workspace files.

Next: Migrating to Vite and Troubleshooting