Day 26 β Quality Assurance: Strict Mode, ESLint, Prettier & Husky
π Day 26 β Quality Assurance: Strict Mode, ESLint, Prettier & Husky
Zero to Hero β Hands-on Angular Tutorial
Today you will learn:
- βοΈ Angular Strict Mode: Creating bulletproof TypeScript.
- βοΈ ESLint: Catching bugs before they happen.
- βοΈ Prettier: Automatic code formatting.
- βοΈ Husky & Lint-Staged: Preventing bad code from being committed.
- βοΈ GitHub Actions: Basic CI Pipeline.
Before we start our final project (Days 27-30), we must ensure our environment enforces Quality. A pro developer doesnβt rely on willpower; they rely on tools. π οΈ
π¦ 1. Angular Strict Mode
If you created your app with ng new, you likely enabled strict mode. If not, enable it now in tsconfig.json.
tsconfig.json:
{
"compilerOptions": {
"strict": true, // The master switch
"noImplicitAny": true, // β No 'any' allowed
"strictNullChecks": true, // β Must handle 'null' and 'undefined'
"strictPropertyInitialization": true // β Properties must be initialized
}
}
Why? It prevents βUnsafeβ coding.
- Bad:
user: User;(Might be undefined at runtime!) - Good:
user: User | null = null;oruser!: User;(I promise to fix it).
π© 2. ESLint (The Linter)
TSLint is dead. Long live ESLint. Add it to your Angular project:
ng add @angular-eslint/schematics
This acts as a βspell checkerβ for code logic.
Common Rules (.eslintrc.json or eslint.config.js):
- no-console: Warns if you leave
console.login code. - component-selector: Enforces
app-prefix. - directive-selector: Enforces camelCase.
Run it:
ng lint
π§ 3. Prettier (The Formatter)
ESLint checks logic. Prettier checks style (Spaces vs Tabs, Semicolons). They work best together.
Step 1: Install
npm install --save-dev prettier
Step 2: Config (.prettierrc)
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"semi": true,
"printWidth": 100
}
Step 3: Run
npx prettier --write .
Your entire codebase is now formatted perfectly. β¨
π₯ 4. Husky & Lint-Staged (The Gatekeeper)
We want to prevent lazy developers (or our future selves) from pushing bad code.
Husky runs scripts before git commit.
Step 1: Install
npx husky-init && npm install
npm install --save-dev lint-staged
Step 2: Configure package.json
"lint-staged": {
"*.{ts,html,scss}": [
"prettier --write",
"eslint --fix"
]
}
Step 3: Update hook (.husky/pre-commit)
npx lint-staged
Result:
Whenever you run git commit -m "update", Husky will:
- Run Prettier on changed files.
- Run ESLint on changed files.
- If they fail, the commit is blocked π.
π« 5. GitHub Actions (Basic CI)
If you use GitHub, create .github/workflows/ci.yml.
name: Angular CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Deps
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm run test -- --no-watch --no-progress --browsers=ChromeHeadless
- name: Build
run: npm run build
Now, every Pull Request gets a green checkmark β or red X β.
π End of Day 26 β What You Learned
Today you automated Quality Assurance:
- βοΈ Strict Mode: Compiler forces safety.
- βοΈ ESLint: Finds bugs automatically.
- βοΈ Prettier: Fixes formatting instantly.
- βοΈ Husky: Blocks bad commits.
- βοΈ CI: Validates code in the cloud.
Your project is now indestructible.
π§ͺ Day 26 Challenge
βBreak and Fixβ
- Create a variable
x;(implicitly any) in a component. - Run
ng build-> Should fail due tostrict: true. - Add a
console.log('test'). - Run
ng lint-> Should warn about console usage. - Mess up the indentation in
app.component.ts. - Run
npx prettier --write .-> Should fix it. - Try to commit the bad code. Husky should stop you!