In this lesson, a late booking for Grace Liu’s party went into door-check’s door list with its party size in quotes, partySize: "3", and the project’s build passed anyway.
Key Points
- Vite doesn’t check types. It removes them from what it bundles for the browser, while your file keeps them, so
vite buildpassed on a booking whose party size was a string where theBookingtype saysnumber. - A wrong type breaks things later. Plain JavaScript carries that string along until something does arithmetic with it. Adding up the four party sizes,
2 + 4 + 1 + "3", gives'73'instead of10. - A green build says nothing about types. The same file failed
npm run check, and the error points to where the expected type comes from:partySize: number;in theBookingtype, on line 4. - Node works the same way. It removes the types from door-check’s export script and runs it, without checking them.
- Only
tscchecks the types. Put the check in front of the build, so the build stops on a type error before Vite runs. No new bundle is built, but thedistfolder still holds the last build’s bundle, so don’t ship that one. - Let CI run the build script. CI is a service that runs your scripts on every push. Set it to run this build script rather than
vite buildon its own, and to deploy the page only when that build passes.
This is the error npm run check reported:
src/door-list.ts:12:47 - error TS2322: Type 'string' is not assignable to type 'number'.
Final Code
Door-check’s scripts in package.json, with the build running the check first:
package.json
"build": "npm run check && vite build",
"check": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p scripts/tsconfig.json"
The && runs vite build only when npm run check passes. With the quotes taken off, Grace Liu’s booking on line 12 of src/door-list.ts passes the check:
src/door-list.ts
{ reference: "LST-0423", name: "Grace Liu", partySize: 3, checkedIn: false },
Next, in Typing Vite’s Environment Variables, you’ll give the checker the types for the settings Vite hands to your page code.