Door-check, in the course’s sample code download (linked on the course page and in the course overview), is the page that Rosa Delgado’s ushers will use at the door of the Lantern Street Theatre. It shows the night’s bookings, a search box for booking references, and check-in buttons. In earlier projects, you checked TypeScript with npm run check and ran it with Node.js or a bundler, using a tsconfig.json written for you.
This project needs more than that file, because its code runs in two places. In this lesson, you’ll learn how tsconfig.json describes where your code runs, give each part of door-check a config that matches where it runs, and check the whole project with one command. Start with a question you can answer from the folder itself: Where does each file run?
See What One Config Decides
You’ll work in door-check for most of this course, changing it on your machine and running npm run check after every change. The coding exercises work differently. You write their code in your browser, and their tests run it in Node.js. The tests check what the code does and look in its source for the types the task asks for. They don’t run tsc, so door-check is where you see the checker’s own output.
The download’s 01-tsconfig folder holds three projects, door-check, ticketing and ticketing-pages, and this course starts in door-check. If you haven’t downloaded it yet, do it now and extract it. Then open a terminal in the extracted folder, install door-check’s packages, and run the check:
$ cd 01-tsconfig/door-check
$ npm install
$ npm run check
> [email protected] check
> tsc --noEmit
No errors. The project’s package.json pins TypeScript to one release line instead of the newest, because typescript-eslint, the ESLint support for TypeScript you’ll add later, supports a new TypeScript release only some time after it comes out.
Now look at what the folder holds. The page code is in src/, and Vite serves and bundles it for the browser. Vite reads its own settings from vite.config.ts, and Vite runs that file in Node.js, so it’s Node.js code too. The ushers’ export script, scripts/export-door-list.ts, runs in Node.js and prints the night’s door list for Mei Chen’s box office:
scripts/export-door-list.ts
import { doorList } from "../src/door-list.ts";
const rows = doorList.map((booking) =>
[booking.reference, booking.name, booking.partySize].join(","),
);
console.log(["reference,name,party", ...rows].join("\n"));
The script imports the same doorList that the page shows, so both agree on who’s coming. You run it with npm run export. It’s a toy export. Joining fields with commas works only when no name contains a comma or a quote, and a real export would quote each field.
The tsconfig.json that came with the project is the one your earlier projects used, with one line added:
tsconfig.json
{
"compilerOptions": {
"module": "nodenext",
"target": "esnext",
"strict": true,
"noEmit": true,
"skipLibCheck": true,
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"erasableSyntaxOnly": true
}
}
The added line, "skipLibCheck", concerns declaration files (.d.ts): files that only describe types, whether packages ship them or you write them. It tells tsc to skip checking those files while still using them to check your code. Door-check needs it for now because Vite’s declaration files rely on Node.js’s types, which the project doesn’t have yet. That’s all you need to know about declaration files for now. A later lesson comes back to them.
What does this file say about where the code runs? It has no "include", so tsc checks every .ts file outside node_modules/ with the same settings. It has no "lib" either, so every file may use the browser’s built-in names, such as document, along with JavaScript’s.
To see what that means, ask the checker whether the export script may use a name that only the browser has. Add a line that reads the page’s title, which a Node.js script can’t access:
scripts/export-door-list.ts
import { doorList } from "../src/door-list.ts";
const rows = doorList.map((booking) =>
[booking.reference, booking.name, booking.partySize].join(","),
);
console.log(`Door list for ${document.title}`);
console.log(["reference,name,party", ...rows].join("\n"));
Run npm run check again, and it still reports no errors. Then run the script:
$ npm run export
> [email protected] export
> node scripts/export-door-list.ts
file:///.../door-check/scripts/export-door-list.ts:7
console.log(`Door list for ${document.title}`);
^
ReferenceError: document is not defined
at file:///.../door-check/scripts/export-door-list.ts:7:30
...
The checker allowed a line that crashes, because one config describes one place, and this project’s code runs in two. Keep line 7 for now, because it’s how you’ll know the new configs work.
Split the Project by Where It Runs
Each part of door-check needs a config that says where its code runs. The page code gets tsconfig.json at the root. The code that Node.js runs, which is the export script and Vite’s config, gets its own scripts/tsconfig.json. Adding "scripts" to one config wouldn’t work, because a config allows the same built-in names in every file it checks. Either the script may use document, or the page can’t.
Replace the root tsconfig.json with this version for the page code:
tsconfig.json
{
"include": ["src"],
"compilerOptions": {
"target": "esnext",
"lib": ["esnext", "dom"],
"module": "esnext",
"moduleResolution": "bundler",
"types": [],
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"erasableSyntaxOnly": true
}
}
Read it from the top. "include" lists which files are in the project, and this one includes everything in src/. "target" is the JavaScript version tsc would write if it emitted files. With "noEmit", it writes none, and Vite decides how the code reaches the browser.
"lib" lists the built-in names the code may use. Here, that means modern JavaScript and the DOM, short for Document Object Model, which is where document and every element type come from.
Like every option here, it only informs the checker, so listing a library doesn’t add its features to a browser or Node.js version that lacks them.
The next two lines describe what loads the modules. "module": "esnext" says the files use import and export, and "moduleResolution": "bundler" tells tsc to find imported files the way a bundler such as Vite does.
"types" lists the @types packages whose global names every file gets. These packages provide types for code that ships without them. The empty list means none adds globals to the page code, though a file that imports a package can still get that package’s types.
Current TypeScript already turns "strict" on, includes no @types packages unless you list them, and treats code as modules resolved the way a bundler does, so several of these lines repeat a default. The project writes them down anyway, because this file is where the next volunteer looks to learn how the code is checked.
Now create scripts/tsconfig.json for the code Node.js runs:
scripts/tsconfig.json
{
"include": ["export-door-list.ts", "../vite.config.ts"],
"compilerOptions": {
"target": "esnext",
"lib": ["esnext"],
"module": "nodenext",
"moduleResolution": "nodenext",
"types": ["node"],
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"erasableSyntaxOnly": true
}
}
Paths in "include" start from the config’s folder, so "../vite.config.ts" reaches up one level to Vite’s config. tsc also checks the files the script imports, so src/door-list.ts is checked under both configs. The "lib" list has no "dom", because Node.js has no page. The "nodenext" setting tells tsc to follow Node.js’s own rules for finding and loading modules, as your earlier projects did.
The "types" line lists "node", and that needs a package. Node.js’s own built-in names, such as console and process, get their types from a package called @types/node instead of from "lib".
Until you install it, tsc stops on this config with error TS2688: Cannot find type definition file for 'node'. Without Node.js’s types, the checker wouldn’t even know console. Install it as a development dependency:
$ npm install --save-dev @types/node
Neither new config has "skipLibCheck". The only one that reads Vite’s declaration files now has Node.js’s types, so those files pass the check. The two configs share most of their lines. They differ in the built-in names, the global types, and the rules for loading modules, which is where a browser and Node.js differ.
To try the configs against a case that isn’t in door-check, use the button below. It opens a chat with the AI mentor and fills in the question. Describe a file, and work out together which config it belongs in:
Check Every Config
A project with two configs needs a check that runs both. The -p flag, short for --project, tells tsc which config to use. Watch out for the shortcut of naming a file instead, as in npx tsc src/main.ts. Naming a file skips tsconfig.json, and current versions of tsc refuse it with error TS5112. Open package.json and change the "check" script to run tsc once for each config:
package.json
"check": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p scripts/tsconfig.json"
The && runs the second check only when the first one passes, so npm run check fails if either run finds an error.
Some of Vite’s templates instead give the root tsconfig.json "files": [] and a "references" list of other configs. A bare tsc --noEmit on that root checks no files at all, because only tsc -b, a build mode this course doesn’t use, follows the "references" list. Two -p runs say exactly what’s checked.
Line 7 is still in the export script. Run the new check:
$ npm run check
> [email protected] check
> tsc --noEmit -p tsconfig.json && tsc --noEmit -p scripts/tsconfig.json
scripts/export-door-list.ts:7:30 - error TS2584: Cannot find name 'document'.
Do you need to change your target library? Try changing the 'lib'
compiler option to include 'dom'.
7 console.log(`Door list for ${document.title}`);
~~~~~~~~
Found 1 error in scripts/export-door-list.ts:7
The page config passed, so the second tsc run caught the error that would crash the script. Read the error before you follow its suggestion. Adding "dom" to "lib" would make the check pass and leave a script that still crashes, because Node.js has no page. The checker is right about the line, so the fix belongs in the code. Delete line 7.
Run npm run check once more:
$ npm run check
> [email protected] check
> tsc --noEmit -p tsconfig.json && tsc --noEmit -p scripts/tsconfig.json
Both checks pass. Every line of door-check, Vite’s config included, is now checked against the place where it runs. If it’s not clear why the browser’s names come from "lib" and Node.js’s from @types/node, the button below opens the mentor with that question and this section attached:
Check Your Understanding
Try these, using door-check as you’ve configured it, before you open the answers.
- Rosa asks for a second Node.js script,
scripts/print-badges.ts, that prints name badges for the night’s guests. Liam adds it with a typo, runsnpm run check, and no error mentions the new file. Why not, and what should he change? - Liam changed only the page code and wants to check only its config. Which command does he run, and what does it leave unchecked that
npm run checkcovers? - Sofia suggests keeping one config: delete
scripts/tsconfig.json, put back the config door-check came with, and set the"check"script back to a singletsc --noEmit. Once she has, what wouldnpm run checkstop catching?
- The root config includes only
src, andscripts/tsconfig.jsononlyexport-door-list.tsand../vite.config.ts, so no config includes the new file andtscnever reads it. Liam should add"print-badges.ts"to the"include"list inscripts/tsconfig.json. npx tsc --noEmit -p tsconfig.jsoncheckssrc/with the page config. It leaves out whatscripts/tsconfig.jsoncovers: the export script, Vite’s config, and the page files the script imports, such assrc/door-list.ts, all checked against Node.js. After changing one of those, Liam still needsnpm run check.- The old config gives every file the browser’s names, so the export script or Vite’s config could use
document, pass the check, and crash in Node.js, like your line 7. The page code would also be checked with Node.js’s module rules instead of the bundler’s.
Door-check now has two configs, one for the page code and one for the code Node.js runs, and npm run check checks both with "strict" on but no additional strictness flags. However, "strict" turns on a family of checks, and some useful ones aren’t in it at all. In the next lesson, Choosing the Strict Flags, you’ll see what each flag catches and pick the ones door-check needs.