ProgramGuru

Understanding Compile-Time vs Runtime

Learn Understanding Compile-Time vs Runtime

Compile time is when TypeScript analyzes source. Runtime is when JavaScript statements actually execute.

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 understanding compile-time vs runtime.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

An annotation such as : number or an interface can be checked and then erased. At runtime, typeof and explicit conditionals can inspect real values; TypeScript types cannot. A network response claiming to be a particular interface has not been validated merely because the source uses as SomeType.

See the concept visually

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

Understanding Compile-Time vs Runtime: illustrated flowCompile time: check; Erase type syntax; Runtime: typeof; Choose branch1Compile time: check2Erase type syntax3Runtime: typeof4Choose branchFollow the change from source values to the final result.
Understanding Compile-Time vs Runtime: illustrated flow. Compile time: check; Erase type syntax; Runtime: typeof; Choose branch.

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

function describe(value: unknown): string {
   if (typeof value === "number") {
     return `number: ${value}`;
   }
   return "not a number";
 }
 console.log(describe(42));
 console.log(describe("42"));

Verified output

number: 42
not a number

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

The input is unknown at check time, so typeof narrows it in the branch. At runtime the first call sees a number and the second sees a string. Their output differs because typeof is a real JavaScript operation.

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

Never write an as assertion expecting it to transform or validate a runtime value. Use a real guard or schema validator where needed.

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

Replace the second argument with null, then with 0. Predict the branch for each before executing.

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

Check your understanding

Which operation checks a runtime value?
Choose one answer. You can try again.

Answer and explanation

typeof value === "number"

typeof is JavaScript; annotations, aliases and assertions are type-system constructs.

Review the key idea

Understanding Compile-Time vs Runtime: An annotation such as : number or an interface can be checked and then erased.

typeof is JavaScript; annotations, aliases and assertions are type-system constructs.

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: TypeScript Syntax →