Lesson 1 of 15

Type Annotations

Type Annotations

TypeScript adds type annotations to JavaScript. A type annotation is a : type after a variable name or parameter that tells TypeScript what kind of value is expected:

let name: string = "Alice";
let age: number = 30;
let active: boolean = true;

TypeScript catches mistakes at compile time:

let score: number = "high";  // Error: Type 'string' is not assignable to type 'number'

Type Inference

You do not always need to write types. TypeScript infers types from values:

let name = "Alice";   // inferred as string
let age = 30;         // inferred as number

Only add annotations when inference is insufficient or unclear.

The Primitive Types

  • string — text values
  • number — integers and floats (no separate int/float)
  • booleantrue or false
  • null — intentional absence of value
  • undefined — uninitialized variable
  • unknown — value of unknown type (safe alternative to any)
  • any — disables type checking (avoid when possible)

Your Task

Declare three typed variables: language (string "TypeScript"), version (number 5), and typed (boolean true). Print each one.

TypeScript loading...
Loading...
Click "Run" to execute your code.