ProgramGuru

How TypeScript Works

Learn How TypeScript Works

Follow the path from a TypeScript source file through checking, JavaScript emission and execution.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind how typescript works.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

The tsc compiler parses the source, checks types, and—unless instructed otherwise—can emit JavaScript. Checking and emitting are separate concerns: a project can use noEmit for checking while another tool builds the JavaScript. By default some compiler errors do not prevent emission, so production workflows often use noEmitOnError.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

How TypeScript Works: illustrated flowSource: count: number; Type-check using tsc; Output: count = 3; Run: 1201Source: count: number2Type-check using tsc3Output: count = 34Run: 120Follow the change from source values to the final result.
How TypeScript Works: illustrated flow. Source: count: number; Type-check using tsc; Output: count = 3; Run: 120.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const count: number = 3;
 const total: number = count * 40;
 console.log(total);

Verified output

120

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

TypeScript checks that count and total are numbers. The emitted JavaScript contains ordinary variable declarations and multiplication. Node executes the JavaScript and prints 120.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

A successful JavaScript run is not evidence that tsc checked the source; some runtimes only remove erasable type syntax.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Compile with tsc --strict --noEmit to check types without generating JavaScript. Then compile normally and inspect the emitted .js file.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

What is erased during ordinary TypeScript compilation?
Choose one answer. You can try again.

Answer and explanation

Type annotations such as : number.

Types inform static checking; runtime operations are preserved.

Review the key idea

How TypeScript Works: The tsc compiler parses the source, checks types, and—unless instructed otherwise—can emit JavaScript.

Types inform static checking; runtime operations are preserved.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Installing Node.js →