Checking and Running a Project

In the video, you watched the checker read code before it ran, then saw the types erased so plain JavaScript could run. Those are two separate jobs. On your own machine, they’re two separate commands, and each one tells you something different.

Dee has a reason to care. Her repair queue script kept saying a day had room for one more repair, so she booked it, and the shop ran out of hours before it ran out of bikes. In this lesson, you’ll set up a small TypeScript project, check and run Dee’s script, and see which command catches her bug. Start with a project folder that has TypeScript in it.

Set Up the Project

The checker is a separate program from Node.js. It’s called tsc, short for TypeScript compiler. It comes in the typescript package on npm. You’ll install it into a project, the same way you’ve installed tools like a linter before.

First, check that your version of Node.js can run TypeScript files. Node.js can run them from version 22.18 on, so any current Node.js release with long-term support (LTS) will do. This lesson uses Node.js 26:

Language: Shell
$ node --version
v26.13.1

Your version number may differ. If it’s older than 22.18, then update Node.js to the current LTS before you go on, because earlier releases need an extra flag to run .ts files or can’t run them at all.

Next, create a folder for the project, move into it, and give it a package.json:

Language: Shell
$ mkdir repair-queue
$ cd repair-queue
$ npm init -y

Now install TypeScript as a dev dependency, a package the project needs while you work on it but not when the program runs:

Language: Shell
$ npm install --save-dev typescript

added 2 packages, and audited 3 packages in 1s

found 0 vulnerabilities

TypeScript is a dev dependency because checking happens on your machine before the code runs. Node.js runs the program later without any help from the typescript package.

The checker also needs to know how strict to be and what kind of JavaScript to expect. That goes in a file called tsconfig.json, and tsc can write a first one for you. You run it with npx, which finds the tsc installed in this project’s node_modules/ folder:

Language: Shell
$ npx tsc --init

Created a new tsconfig.json

You can learn more at https://aka.ms/tsconfig

Open the new file, and you’ll find about forty lines, many of them comments and options you won’t need yet. Replace everything in it with this shorter version, which keeps only what this course uses:

Language: JSON Filename: tsconfig.json
{
  "compilerOptions": {
    "module": "nodenext",
    "target": "esnext",
    "strict": true,
    "noEmit": true,
    "verbatimModuleSyntax": true,
    "erasableSyntaxOnly": true
  }
}

Each line tells tsc one thing. "module" tells the checker to follow Node.js’s rules for modules, and "target" tells it to expect modern JavaScript. "strict" turns on TypeScript’s strict family of checks, and you’ll see one of them at work shortly.

"noEmit" tells tsc to check only. It can also write JavaScript files, but Node.js runs .ts files itself, so you don’t need them. The last two options restrict your code to the TypeScript syntax that Node.js can run by removing the types. That’s all you need about those two for now.

Last, open package.json and make two changes. Set "type" to "module", so Node.js treats your files as ES modules, and replace the "scripts" entry with a "check" script:

Language: JSON Filename: package.json
{
  "name": "repair-queue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "check": "tsc --noEmit"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "module",
  "devDependencies": {
    "typescript": "^7.0.2"
  }
}

From now on, npm run check runs tsc --noEmit on every .ts file in the folder. The --noEmit flag repeats what tsconfig.json says, so anyone reading the script can see that it checks and writes nothing. Your typescript version may be newer than the one shown, and that’s fine.

Run the Queue With Node.js

With the project ready, it’s time for Dee’s script. It keeps the day’s repair queue, adds up the booked hours, and answers the question Dee asks before she takes a new job: Does it fit in an eight-hour day?

Every JavaScript program is also a TypeScript program the checker can read, so you can save Dee’s JavaScript as it is, with a .ts extension. Create queue.ts in the project folder with this code:

Language: TypeScript Filename: queue.ts
 1const repairQueue = [
 2  { bike: "Amara's commuter", hours: 1.5 },
 3  { bike: "Luis's road bike", hours: 3 },
 4  { bike: "Mei's cargo bike", hours: 2.5 },
 5];
 6
 7const bookedHours = repairQueue.reduce(
 8  (sum, ticket) => sum + (ticket.hour || 0),
 9  0,
10);
11
12function canBook(jobHours, booked, dayHours) {
13  return booked + jobHours <= dayHours;
14}
15
16console.log("Booked hours:", bookedHours);
17console.log("Fits a 3-hour job:", canBook(3, bookedHours, 8));

The .reduce() call adds each ticket’s hours to a running sum. The || 0 is there in case a ticket has no hours yet. Then canBook() asks whether a new three-hour job fits on top of what’s booked.

Run the file with Node.js, the same way you’d run a .js file:

Language: Shell
$ node queue.ts
Booked hours: 0
Fits a 3-hour job: true

Node.js ran the TypeScript file without an error or a warning. So, does a clean run mean the file has no type errors? Predict what npm run check will say about queue.ts, then run it:

Language: Shell
$ npm run check

The checker finds four errors. The first one is a real bug (its long line wrapped for display):

Language: Program Output
> [email protected] check
> tsc --noEmit

queue.ts:8:34 - error TS2551: Property 'hour' does not exist on type
'{ bike: string; hours: number; }'. Did you mean 'hours'?

8   (sum, ticket) => sum + (ticket.hour || 0),
                                   ~~~~

  queue.ts:2:31 - 'hours' is declared here.
    2   { bike: "Amara's commuter", hours: 1.5 },
                                    ~~~~~~~~~~

...

Found 4 errors in the same file, starting at: queue.ts:8

If you expected Node.js to refuse a file with type errors, that’s a natural guess, but Node.js never checks types. When it runs a .ts file, it removes the types and runs the JavaScript that’s left, whatever the checker would say about it. Only tsc, here through npm run check, reports type errors. A file that runs tells you nothing about whether its types are right.

That’s the model to keep—two commands with two jobs, both starting from the same file:

From queue.ts, npm run check checks the types and reports errors, while node queue.ts removes the types and runs it unchecked.
Checking and Running Are Separate Jobs

npm run check reads your code and reports type errors, but it never runs anything or writes any files. node queue.ts runs your code, but it never checks it. Your editor likely runs the same checker as you type, so you may already see a red underline under hour, but the terminal’s output is the one to trust.

If you edit a file and run it again and again, when should you check it? The button below opens a chat with the AI mentor, with that question filled in for you to send. Tell it how you like to work, and it can help you find a rhythm.

Whatever rhythm you settle on, run npm run check before you trust a change, because a run that works hasn’t checked anything.

Read and Fix What the Checker Found

Read the first error before you change anything. It starts with where the problem is: queue.ts, line 8, column 34. Then comes the error code, TS2551, and the message.

The property hour doesn’t exist on the type { bike: string; hours: number; }, which is the shape of each ticket in the queue. The checker even guesses the fix: “Did you mean ‘hours’?”

How does the checker know what a ticket looks like? You never told it. It worked out the shape from the array of objects at the top of the file, and it knows .reduce() passes those objects to the callback as ticket. That’s all you need to know about how the checker works out types for now.

The typo explains Dee’s overbooked day. Every ticket.hour was undefined, so || 0 turned each one into 0, and the queue looked empty. With seven hours already booked, a three-hour job never fit, but the script said it did.

Fix line 8 so it reads hours, the property the tickets have:

Language: TypeScript Filename: queue.ts
 1const repairQueue = [
 2  { bike: "Amara's commuter", hours: 1.5 },
 3  { bike: "Luis's road bike", hours: 3 },
 4  { bike: "Mei's cargo bike", hours: 2.5 },
 5];
 6
 7const bookedHours = repairQueue.reduce(
 8  (sum, ticket) => sum + (ticket.hours || 0),
 9  0,
10);
11
12function canBook(jobHours, booked, dayHours) {
13  return booked + jobHours <= dayHours;
14}
15
16console.log("Booked hours:", bookedHours);
17console.log("Fits a 3-hour job:", canBook(3, bookedHours, 8));

Run it again, and the numbers match the queue:

Language: Shell
$ node queue.ts
Booked hours: 7
Fits a 3-hour job: false

Now run the check again to see what’s left:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

queue.ts:12:18 - error TS7006: Parameter 'jobHours' implicitly has an 'any' type.

12 function canBook(jobHours, booked, dayHours) {
                    ~~~~~~~~

...

Found 3 errors in the same file, starting at: queue.ts:12

The three errors left are all TS7006, one for each parameter of canBook(): jobHours, booked, and dayHours. Unlike ticket, these parameters get their values from calls elsewhere, so the checker has nothing to work out their types from. Without strict mode, it would assign each parameter the type any, meaning “could be anything, so don’t check it,” without a warning. Strict mode won’t let that slide, so it asks you to say what each parameter holds.

If it’s not yet clear why ticket gets a type and jobHours doesn’t, the button below opens the mentor with that question and this section attached. Ask it to compare where each value comes from, one step at a time.

The array gave ticket its shape. The checker never works out a parameter’s type from the calls to a function, not even from the call on line 17, so nothing tells it what the parameters of canBook() hold. Leave those three errors for now. Telling the checker is exactly what annotations are for.

Check Your Understanding

Try these before you open the answers. They use the project you built here.

  1. Dee’s brother says, “The script ran fine, so the types must be right.” What would you tell him?

  2. You start a new project for the shop’s parts list. Which commands add TypeScript to it and create its first tsconfig.json?

  1. Running and checking are separate jobs. Node.js runs a .ts file whatever its types say, so a clean run proves nothing about them. Run npm run check to find type errors.
  2. Inside the project folder, run npm init -y if there’s no package.json yet, then npm install --save-dev typescript, then npx tsc --init. After that, trim tsconfig.json and add a "check": "tsc --noEmit" script, as you did here.

Your project now checks and runs, and the checker has one more request: it wants to know what the parameters of canBook() hold. In the next lesson, Annotating Functions and Variables, you’ll tell it and see what that buys you on both sides of a function call.

Become a Member to join the conversation.