JavaScript · Advanced level

Advanced level · Module 4: TypeScript Essentials · Lesson 1 of 4

Why Types: Setting Up TypeScript

Understand what static types add to JavaScript and set up the TypeScript compiler in strict mode.

In this lesson you will

  • Explain the benefits and limits of static typing
  • Configure tsc with a strict tsconfig.json

Key concepts

static typingtsctsconfig.json and stricttype checking vs running

The lesson

TypeScript is JavaScript with type annotations. The compiler checks the types before the code runs, catching mistakes such as misspelt properties, missing arguments or using a possibly undefined value, then outputs plain JavaScript.

Types do not exist at run time: data from users, files and APIs must still be validated. TypeScript checks your code's consistency, not the outside world.

Install TypeScript as a dev dependency, create tsconfig.json and enable "strict": true. Run npx tsc --noEmit to type-check without producing files, and add it to your npm scripts.

Step by step

  1. Install TypeScript and create tsconfig.json with strict enabled.
  2. Rename one module to .ts.
  3. Run the type checker and read the errors.
  4. Fix the errors and add a typecheck script.

Practice activity

Set up TypeScript for the visit tracker and type-check one module with zero errors.

Expected result: npm run typecheck passes for the converted module.

Check your understanding

  1. Do TypeScript types exist when the code runs?

    Show answer

    No; they are removed during compilation.

  2. What does "strict": true enable?

    Show answer

    A set of stricter checks, including null checks and no implicit any.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.