Type Annotation
In one line
A type annotation is an explicit declaration of a variable's or parameter's type using a colon followed by the type name.
In simple words
A type annotation tells TypeScript what type a value should be. You write it after a colon: let x: number = 5. The annotation is a compile-time hint — it is removed when the code is compiled to JavaScript.
Type annotations are optional in many cases because TypeScript can infer types automatically. However, explicit annotations are useful for function parameters (where inference is not possible), for making code more readable, and for catching errors where the wrong type is assigned.
Annotations are especially important for function parameters, since TypeScript cannot infer parameter types from usage. They are also helpful for public APIs where you want to be explicit about the expected types.
Example
let age: number = 25;
let name: string = "Bob";
let isActive: boolean = true;
function greet(person: string): string {
return `Hello, ${person}`;
}Each variable and function parameter has an explicit type annotation after a colon.
Common confusions
Confused with: type assertion
The difference: An annotation declares a type on a new variable; an assertion overrides the type of an existing value using 'as'.
Related terms
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.