Loading video player…

Adding a Type Check to a Vite Project

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 build passed on a booking whose party size was a string where the Booking type says number.
  • 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 of 10.
  • 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 the Booking type, 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 tsc checks 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 the dist folder 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 build on its own, and to deploy the page only when that build passes.

This is the error npm run check reported:

Language: Shell
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:

Language: JSON Filename: 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:

Language: TypeScript Filename: 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.

00:00 Tonight, Rosa Delgado’s ushers check guests in with door-check, so the page has to go out before the doors open. In this video, you’ll find out what door-check’s build really checks on the way.

00:12 Mei Chen at the box office has sent one more booking for tonight, Grace Liu’s party of three. A volunteer pastes it into the door list from the box office’s spreadsheet, and the party size comes across in quotes, as text.

00:27 Before the page goes live, the volunteer runs the project’s build script. It runs Vite’s build, which bundles the page code for the browser, and it’s the last thing anyone runs before the ushers get the page.

00:40 The build passes. Vite transformed five modules and wrote the bundle to the dist folder. A green build is all anyone sees, and it looks as if the new booking is fine.

00:52 But what did that build actually check?

00:56 Now run npm run check on the same file. It runs the checker, tsc, once for each of door-check’s two configs. The checker reports TS2322 on line 12. Type string is not assignable to type number.

01:14 Read on, and it says where the expected type comes from: partySize is declared as a number in the Booking type. The new booking’s party size is the string "3".

01:26 The build passed because Vite turns TypeScript into JavaScript by removing the types. In what Vite bundles, the Booking type disappears, and so does the annotation on the door list.

01:38 Your file keeps its types. What’s left is plain JavaScript. It keeps a party size in quotes as a string until something does arithmetic with it. Add up the four party sizes, and instead of ten you get the string '73'.

01:54 Vite never asks whether that string fits the type. It leaves type checking to you, so a green build says nothing about types.

02:03 Node runs door-check’s export script the same way. It removes the types and never checks them.

02:11 The fix is in package.json. Change the build script so it runs npm run check first, then Vite’s build. The double ampersand runs the second command only when the first one passes, as it already does between door-check’s two checks.

02:28 Run the build again. This time, the check reports the same error, and the build stops there. Vite never runs, so no new bundle is built. The dist folder still holds the first build’s bundle, though, so don’t send that one to the ushers.

02:45 A party size is a count, so the type is right and the data is wrong. Take the quotes off, so the party size is the number three. Run the build once more, and the checks pass before Vite builds the page.

02:58 CI is a service that runs your scripts on every push. If the project has it, set it to run this build script, not Vite’s build on its own. Then let it deploy the page only when that build passes.

03:13 So Vite removes your types and bundles the JavaScript for the browser. Node removes them and runs the script. Only tsc checks them. With the check in front of the build, a green build means the types passed too.

03:27 Next, in Typing Vite’s Environment Variables, you’ll give the checker the types for the settings Vite hands to your page code.

Become a Member to join the conversation.