Type Annotation (TypeScript)

A type annotation is the part after a colon that tells TypeScript what type a variable, parameter, or return value should be, such as `: number` or `: string`. It lets the compiler check your code for type mistakes.

TypeScript

In plain JavaScript a variable can hold anything and change type at will. A TypeScript annotation pins it down: once you write `let count: number`, assigning a string becomes an error.

You don't always have to write annotations — TypeScript can often infer the type for you — but they are essential for function parameters, which can't be inferred from usage.

Syntax

let name: type = value;

Examples

TypeScript

let count: number = 5;
let username: string = "Ada";
let isActive: boolean = true;

TypeScript

function add(a: number, b: number): number {
  return a + b;
}

Learn it in a lesson

See also

Back to the Dictionary