Module 03 · Lesson 3.4
Linting, Formatting and Type Checks
On this page
- What you will learn
- What linting, formatting and type checks catch
- Worked scenario: a real type error in a Tidewell rule
- One command to run every check
- Step-by-step workflow: add checks to a project
- What the assistant does and what you do
- Conclusion and your next step
- Reusable prompt
- Expected output
- Common errors and fixes
- Validation checklist
- Hands-on exercise
- Knowledge check
- Frequently asked questions
- References
A good proofreader finds typing slips before a letter is posted. A project needs the same. Machines can spot many mistakes in seconds, long before a person reads the code. That leaves reviewers free to judge whether the code does the right thing.
In this lesson you add three automatic checks to the Tidewell project. One looks for risky habits, one fixes layout and one checks that data is used the right way. You read a real error message from the TypeScript compiler. You also learn how to stop an assistant from silencing a check instead of fixing the problem.
What you will learn
- Explain what linting, formatting and type checking each catch, and what they miss.
- Turn on TypeScript strict mode and read a real type error.
- Add scripts so that one command runs every check.
- Spot an assistant silencing a check instead of fixing the cause.
Before you start
- Completed Configuration and Secrets, or have a Next.js project with TypeScript.
- Access to any capable AI assistant.
- Optional: Node.js and the project from earlier lessons, if you want to run the examples.
What linting, formatting and type checks catch
Each check looks for a different kind of mistake. They are small tools that run without you reading anything. If you run all three on every change, a whole class of errors never reaches a reviewer.
- Linter
- A tool that reads your code and warns about risky habits, like a proofreader who flags a sentence that is probably a mistake. ESLint is the common one for JavaScript and TypeScript.
- Formatter
- A tool that rewrites the layout of your code, such as spacing and line breaks, to one agreed style. It is like a typesetter. It never changes what the code does. Prettier is a common one.
- Type check
- A check that each piece of data is used for what it is. It stops you from using a piece of text where a date is needed, like a spell-checker for kinds of data. TypeScript does this.
- Strict mode
- A TypeScript setting that turns on its strictest checks, including a check for values that might be missing.
| Check | Catches | Example | Does not catch |
|---|---|---|---|
| Linting | Risky or unused code, likely mistakes. | A variable that is never used. | Wrong business rules. |
| Formatting | Inconsistent layout. | Mixed spacing and line breaks. | Anything about meaning. |
| Type check | Data used in the wrong way. | Text passed where a date is needed. | A date that is wrong but valid. |
None of these prove that the code is right. They prove that it is tidy and that its pieces fit together. Tests, which arrive in Module 7, check behaviour.
Worked scenario: a real type error in a Tidewell rule
Here is a small piece of the booking rules. It decides whether a patient may change a booking online. The practice manager confirmed that an appointment must start at least 24 hours from now, and exactly 24 hours is allowed. The rule lives in src/features/bookings/rules.ts.
// Business rule confirmed by the practice manager on 2026-11-19: online changes
// are allowed only if the appointment starts at least 24 hours from now.
export const NOTICE_PERIOD_HOURS = 24;
const MS_PER_HOUR = 60 * 60 * 1000;
export function canChangeOnline(startsAt: Date, now: Date): boolean {
const hoursAway = (startsAt.getTime() - now.getTime()) / MS_PER_HOUR;
return hoursAway >= NOTICE_PERIOD_HOURS;
}Tested with the TypeScript compiler and run. The comment points to the decision.
What each part does
NOTICE_PERIOD_HOURSholds the confirmed rule in one place.MS_PER_HOURturns hours into milliseconds, which is how dates count time.canChangeOnlinetakes the start time and the current time, so a test can pass in a fixed 'now'.>=means exactly 24 hours counts as allowed.
Now imagine a hurried change, perhaps by an assistant, that calls this function badly. This file makes two classic mistakes on purpose.
import { canChangeOnline } from "./rules";
const now = new Date("2026-12-01T09:00:00Z");
// Mistake 1: a text value where a Date is needed.
console.log(canChangeOnline("2026-12-02T09:00:00Z", now));
// Mistake 2: a value that might be missing, used as if it were always there.
function firstSlot(slots: Date[]): Date | undefined {
return slots[0];
}
console.log(canChangeOnline(firstSlot([]), now));Two deliberate mistakes. The first passes text instead of a date. The second passes a value that may be missing.
Run the compiler in check-only mode. The --noEmit flag means it only checks and writes no files. Here is what it printed.
npx tsc --noEmitsrc/features/bookings/rules.broken.ts(6,29): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Date'.
src/features/bookings/rules.broken.ts(12,29): error TS2345: Argument of type 'Date | undefined' is not assignable to parameter of type 'Date'.
Type 'undefined' is not assignable to type 'Date'.How to read the error
rules.broken.ts(6,29)is the file, the line (6) and the column (29).error TS2345is the error's code. You can search it online.- The first message says text was given where a date is needed.
- The second says the value might be
undefined, so it may be missing. This one is caught only because strict mode is on. - The fix is in your code, not in the check. Pass a real date, and handle the case where there is no slot.
With the mistakes fixed, the working rule gives the answers you expect. A booking exactly 24 hours away is allowed, and one a minute short is not. This is the output of a small file that checks two start times against a fixed 'now' of 1 December 2026 at 09:00.
import { canChangeOnline } from "./rules";
const now = new Date("2026-12-01T09:00:00Z");
console.log(canChangeOnline(new Date("2026-12-02T09:00:00Z"), now)); // exactly 24 hours
console.log(canChangeOnline(new Date("2026-12-02T08:59:00Z"), now)); // 1 minute shorttrue
falseOne command to run every check
Three commands are easy to forget. So you add a fourth that runs them all. The scripts part of package.json gives each command a short name. The generator may already add some, and their exact form varies by version, so keep what works and add what is missing.
{
"scripts": {
"dev": "next dev",
"build": "next build",
"lint": "eslint",
"format": "prettier --write .",
"format:check": "prettier --check .",
"typecheck": "tsc --noEmit",
"check": "npm run lint && npm run format:check && npm run typecheck"
}
}The check scripts used across the Tidewell project.
npm run lintruns ESLint.npm run format:checkasks Prettier whether the files match its style. It changes nothing.npm run formatrewrites the files to match. Use it when the check fails.npm run typecheckruns the compiler in check-only mode.npm run checkruns the three checks in order and stops at the first failure.
Strict mode is a setting in tsconfig.json. The Next.js generator normally turns it on. Open the file and confirm it, and do not take it on trust.
{
"compilerOptions": {
"strict": true,
"noEmit": true
}
}ESLint and Prettier are not run in this lesson's tested examples, so no output is pasted. Commands and messages differ by version, so follow the current guides: Getting Started with ESLint (opens in a new tab), What is Prettier? (opens in a new tab) and the TypeScript strict option (opens in a new tab). Keep the setup small. Use the defaults first, and add a rule only when a mistake keeps coming back.
npm run checkRun all checks with one command.
> tidewell-booking@0.1.0 check
> npm run lint && npm run format:check && npm run typecheck
(each tool prints its own summary; the exact text varies by tool and version)Step-by-step workflow: add checks to a project
Step 1: Turn on strict mode first
Check that
strictis true intsconfig.json. It is far easier to start strict than to switch on later.What you should see: The compiler reports possibly missing values from the first file.
Step 2: Get the linter running
Keep the generator's ESLint setup. Run it and read each warning. Fix real ones and ask why before turning any off.
What you should see: The linter runs and its output is clean or explained.
Step 3: Add the formatter
Install Prettier and run
npm run formatonce, in one separate commit, so that the style change does not mix with other changes.What you should see: Every file follows one style, and later diffs show only real changes.
Step 4: Add the scripts
Add
lint,format:check,typecheckandcheckas shown above.What you should see:
npm run checkruns and passes.Step 5: Break something on purpose
Add a deliberate type error and a deliberate style slip. Confirm that
npm run checkfails and names both. Then undo them.What you should see: You have seen the checks fail, so you can trust them when they pass.
Step 6: Tell everyone, including the assistant
Write
npm run checkin the README and in the assistant context file. Say that it must pass before a task is done.What you should see: Everyone runs the same single command.
- Change
By you or an assistant
- Format
One agreed style
- Lint
Risky habits
- Type check
Data used correctly
- Review
Does it do the right thing?
Machines handle tidiness, so a reviewer can think about behaviour.
What the assistant does and what you do
| Task | Assistant's part | Your part |
|---|---|---|
| Setting up the tools | Suggests configuration and scripts. | Check them in the tools' own docs. Settings change by version. |
| Fixing a reported error | Proposes a change to the code. | Read the change. Check that it fixes the cause. |
| Explaining an error | Says in plain words what it means. | Run the check again to confirm. |
| Deciding which rules to turn off | Not its job. | Decide, and write down why. |
| Declaring the work done | Reports that checks pass. | Run npm run check yourself. |
Conclusion and your next step
Linting spots risky habits, formatting settles layout and type checks catch data used the wrong way. Strict mode makes the type check much more useful. One npm run check command runs them all, and you have seen it fail on purpose. Reading the error and fixing the cause beats silencing it.
Your next step: tell the assistant about these commands and your other rules in one place. Continue with Assistant Context Files.
For a longer look at reviewing generated code, read AI Code Review: Catching Bugs Before They Ship and the AI-Generated Code Review Checklist.
Reusable prompt
Explain this error, fix the cause
Gets a plain explanation of a compiler or lint error and a fix that changes the code, not the check.
You are helping me fix a failing check in a TypeScript project. Explain in plain words first. Then propose a fix.
Project: tidewell-booking (fictional clinic booking app). TypeScript strict mode is ON. Rules: do not use the type any, do not add comments that tell the compiler or the linter to ignore a line, and do not change tsconfig or lint settings.
Failing command: npx tsc --noEmit
Output:
src/features/bookings/rules.broken.ts(6,29): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Date'.
Function being called (do not change its signature):
export function canChangeOnline(startsAt: Date, now: Date): boolean
Tasks:
1. Explain what the error means in two or three short sentences, with a real-life comparison.
2. Say where the real mistake is: the call or the function.
3. Show the smallest change that fixes the cause. If the data starts as text, show how to turn it into a date and what to do if the text is not a valid date.
4. Say which command I should run to confirm the fix.Show the reusable template
Replace each {{placeholder}} with your own details.
You are helping me fix a failing check in a TypeScript project. Explain in plain words first. Then propose a fix.
Project: {{project_name}}. TypeScript strict mode is {{strict_on_or_off}}. Rules: do not use the type any, do not add comments that tell the compiler or the linter to ignore a line, and do not change tsconfig or lint settings.
Failing command: {{command}}
Output:
{{error_output}}
Relevant code (do not change its signature):
{{relevant_code}}
Tasks:
1. Explain what the error means in two or three short sentences, with a real-life comparison.
2. Say where the real mistake is: the call or the function.
3. Show the smallest change that fixes the cause.
4. Say which command I should run to confirm the fix.Expected output and limitations
A plain explanation with a comparison, a statement of where the real mistake is, a small code change that does not use any or an ignore comment, and the command to run to confirm.
Meaning: the function wants a Date, like a form wants a proper calendar date, but the call passes plain text. The compiler is refusing to guess.
Where: the call, not the function.
Fix: turn the text into a date before the call, and handle bad text:
const startsAt = new Date(text);
if (Number.isNaN(startsAt.getTime())) { /* report invalid input */ }
canChangeOnline(startsAt, now);
Confirm: run npm run typecheck, then npm run check.Limitations to keep in mind
- The assistant may fix the symptom, for example by adding a cast. Check that the change addresses the cause.
- It may suggest turning off a rule. Your prompt rules forbid that, so check the answer follows them.
- Its explanation of an error code can be wrong. Run the command again to see the real result.
Common errors and how to fix them
The check passes on one machine and fails on another.
Why it happens: Different tool versions are installed, or a check is run from the wrong folder.
Fix: Install from the lock file, run npm run check from the project root and keep the scripts in package.json.
Formatting changes bury the real change in a review.
Why it happens: The formatter was run on many files together with a code change.
Fix: Format once in a separate commit, then keep npm run format:check passing so later changes stay small.
Errors disappear after a change, but the bug remains.
Why it happens: An any type, an ignore comment or a switched-off rule hid the problem.
Fix: Search the diff for these. Replace each with a proper fix, or keep it with a written reason.
Validation checklist
Tick each item once you have confirmed it.
Hands-on exercise
Checks for the Ferngate parts tracker
Ferngate's parts tracker has a function recordPartUsed(partId: number, quantity: number). Write a tiny call that breaks it in two ways, predict the compiler's messages, then write the fix. Add the check scripts to a scripts excerpt for the project.
You will need
- The function signature above.
- The scripts excerpt from this lesson.
- Ferngate's facts: mechanics share one tablet, and quantities arrive from a form as text.
Acceptance criteria
Your work is done when every one of these is true.
Hints (2)
- Ask what a number looks like when a form gives you text such as 'two' or an empty box.
- If you can run the compiler, compare its real output with your prediction.
Show a model answer
A strong answer predicts one error about text not being assignable to a number, and another about a possibly missing value, both on the lines of the calls. The fix converts the text with a number conversion, tests that the result is a real number and handles failure by reporting invalid input instead of calling the function. The scripts excerpt matches the lesson. The answer leaves the types intact and does not cast or silence anything, which is the point: the checks protect the shared tablet from bad data.
Knowledge check
Choose an answer, then check it. Nothing is recorded — this is for you.
Show the answer and explanation
Answer: B. The type check knows what kind of data each function wants. A formatter only changes layout, so it cannot see the mistake. Tests can catch it too, but later and only if someone wrote the case. The compiler catches it at once.
Show the answer and explanation
Answer: B. The check passes only because it has been told to look away. That is tempting when you are in a hurry, but the bug is still there and now invisible. Putting it off usually means it is never fixed.
Show the answer and explanation
Answer: B. A claim that a check passed may be out of date or wrong, but that does not mean assistants always lie, which overstates it. Some assistants can run commands and some cannot. Either way, your own run is the evidence.
Frequently asked questions
What is the difference between a linter and a formatter?
A linter warns about risky or unused code. A formatter only changes layout, such as spacing and line breaks, and never changes what the code does. Many projects use both, so they do not argue about style.
What does TypeScript strict mode do?
It turns on TypeScript's strictest checks. One of them stops you from using a value that might be missing as if it were always there. Starting a project in strict mode is much easier than switching it on later.
How do I run ESLint and Prettier in a Next.js project?
Add scripts to package.json, such as lint and format:check, and run them with npm run. The exact commands depend on your tool versions, so follow the current ESLint and Prettier guides. Then add one check script that runs them all.
Why does my AI assistant keep using the any type?
It is a quick way to make a type error go away. But it switches off checking for that value. Tell the assistant in your prompt and context file not to use it, and search the diff for it.
- Written by
- PromptFable Editorial
- Last updated 19 Nov 2026
- Review
- Reviewed by Waseem Nadir, Technical Reviewer
- Last reviewed 27 Nov 2026
References
Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.
- Getting Started with ESLint - ESLint - Pluggable JavaScript Linter(opens in a new tab)
ESLint · Official documentation · How to set up and run the linter.
- What is Prettier? · Prettier(opens in a new tab)
Prettier · Official documentation · What the formatter does and how to configure it.
- TypeScript: TSConfig Reference - strict(opens in a new tab)
Microsoft · Official documentation · What strict mode turns on.
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.