Skip to Content
Course content

2: Setting Up the TypeScript Compiler

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of the TypeScript compiler as a rigorous building inspector. Imagine you're designing a house. You have your blueprints (your TypeScript code), which specify exactly where the plumbing goes and which walls are load-bearing. But here's the catch: the actual construction crew (the browser or Node.js) doesn't know how to read blueprints. They only understand raw materials and physical labor (JavaScript).

The compiler is that inspector who walks through your blueprints before a single nail is driven. If you accidentally drew a toilet in the middle of the kitchen, the inspector points it out immediately. He doesn't let you start building until the plans make sense. Once he signs off, he "translates" those blueprints into a set of simple, direct instructions that the crew can actually follow.

Getting the inspector on your machine

Before we can catch any errors, we need the tool itself. We use npm for this. I usually recommend installing it globally if you're just learning, so you can call it from anywhere in your terminal.

npm install -g typescript

Once that's done, you have access to the tsc command. That's the "TypeScript Compiler." It's the engine that does all the heavy lifting we just talked about.

Turning blueprints into reality

Let's look at a real scenario. Say you're building a simple price calculator for an e-commerce site. You create a file called calculator.ts. In this file, you've decided that a Product must always have a price that is a number.

interface Product {
    name: string;
    price: number;
}

function calculateTax(item: Product) {
    return item.price * 0.15;
}

// Oops! I accidentally put the price in quotes
const myLaptop = { name: "MacBook", price: "1200" }; 

calculateTax(myLaptop);

If this were plain JavaScript, you wouldn't know anything was wrong until you ran the code and got a weird result (or a crash). But when you run tsc calculator.ts in your terminal, the compiler steps in. It'll scream at you: "Type 'string' is not assignable to type 'number'."

It refuses to create the .js file until you fix that quote mark. Once you change "1200" to 1200 and run tsc again, it generates a clean calculator.js file that is stripped of all the type definitions and ready for the browser to execute.

Telling TypeScript how to behave

Running tsc filename.ts is fine for one file, but it becomes a nightmare when you have fifty. You don't want to manually list every file, and you certainly don't want to pass a dozen configuration flags every time you compile.

This is why we use a tsconfig.json file. It's essentially the "Rulebook" for your project. You create it by running:

tsc --init

This generates a JSON file where you can toggle settings. For example, I often enable "strict": true. It's like hiring the most annoying, pedantic inspector possible. It'll complain about things you might think are "fine," but in my experience, those complaints usually save you from a production bug that would have taken three hours to debug on a Friday afternoon.

With this file in your root folder, you no longer need to specify filenames. You just type tsc, and the compiler looks at the rulebook, finds all the .ts files in your project, and compiles them all at once.




📋 Practical Task

Configuring the Project Order Processor

You've been handed a project that is failing to compile because the compiler settings are too loose, and there's a type mismatch in the order logic. Your goal is to set up the environment and fix the error.

Requirements:

  • Create a folder named order-processor.
  • Inside that folder, initialize a TypeScript configuration file using the CLI.
  • Open the tsconfig.json and ensure "strict": true is enabled.
  • Create a file named orders.ts with the following code:
interface Order {
    id: number;
    total: number;
}

function printReceipt(order: Order) {
    console.log(`Order ${order.id} total is $${order.total}`);
}

const currentOrder = { id: 101, total: "50.00" };
printReceipt(currentOrder);

Your Task: Run the compiler tsc. Observe the error in the terminal, fix the type mismatch in orders.ts so it complies with the Order interface, and successfully compile it into orders.js.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.