Type Annotations
A type annotation is a note you attach to a variable, parameter, or return value that tells the TypeScript compiler what kind of value belongs there. The compiler checks your code against that note, then deletes it before the JavaScript runs.
Detailed Explanation
JavaScript does not care what type a value is. You can hand a string to a function that wanted a number, and nothing complains until something breaks three files away. TypeScript's answer is the annotation: a small piece of syntax, written after a colon, that states your intent in a form a machine can check.
let retries: number = 3;
: number is the annotation. It is a claim you are making. From that line onward the compiler holds you to it, and it holds everyone who touches retries to it too.
Annotations live in four common positions: variable declarations, function parameters, function return types, and class fields. Parameters are the important case. When the compiler checks a function body, it cannot see who will call that function, so it has nothing to guess from. An unannotated parameter is therefore a hole in your type coverage, which is exactly what the noImplicitAny flag exists to report.
You do not need to annotate everything. TypeScript reads the initializer and works out the type on its own, which is called inference. const port = 8080 already has type number, and writing : number adds nothing. The working rule is: annotate the boundaries, infer the insides. Boundaries are function signatures, exported values, and anything crossing into your program from outside. Insides are local variables whose type is obvious from the line above.
Annotating a return type is optional but often worth it. Without it, the return type is whatever the body happens to produce, so a mistake inside the function silently changes the function's public contract and the error surfaces at the call site instead. With an annotation, the error is reported on the line that is actually wrong.
The part beginners miss most: annotations are erased. tsc strips every annotation during compilation, so the emitted JavaScript has no record of them. There is no runtime type check anywhere. This matters the moment data arrives from outside your program:
const user: User = await res.json(); // compiler is satisfied, reality is not
That annotation is a promise you have not verified. res.json() returns any, and any is assignable to anything, so the compiler steps aside. If the server sends a different shape, you get an undefined property at 3 a.m., not a compile error. Untrusted input needs a real runtime check — a type guard or a validation library — and the annotation only describes what you hope came back.
The costs are modest. Annotations add characters to read, and over-annotating obvious locals creates noise that has to be updated whenever the type changes. Excessive annotation can also hide useful information: annotating a variable with a wide type throws away the narrower type inference had already worked out. None of it affects runtime speed or memory, because none of it survives to runtime.
Code Examples
1. Parameters and return type annotated; locals inferred.
// 1. Parameters and return type annotated; locals inferred.
function formatPrice(cents: number, currency: string): string {
const dollars = cents / 100; // inferred as number, no annotation needed
return `${dollars.toFixed(2)} ${currency}`;
}
formatPrice(1999, "USD"); // "19.99 USD"
formatPrice("1999", "USD");
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.2. Annotating wider than necessary throws away information.
// 2. Annotating wider than necessary throws away information.
const inferred = "GET"; // type is the literal "GET"
const annotated: string = "GET"; // type is string; the literal is forgotten
function send(method: "GET" | "POST") {}
send(inferred); // fine
send(annotated);
// Error: Argument of type 'string' is not assignable to parameter of type '"GET" | "POST"'.3. Annotations do not exist at runtime.
// 3. Annotations do not exist at runtime.
function double(n: number): number {
return n * 2;
}
// Compiles to plain JS with no checks:
// function double(n) { return n * 2; }
console.log(double(("5" as unknown) as number)); // "55", not 10Common Gotchas
A colon in destructuring is renaming, not annotating
`const { name: string } = user` does not say "name is a string". It pulls out `user.name` and calls the new local variable `string`. To annotate a destructured value, annotate the whole pattern: `const { name }: { name: string } = user`.
An annotation is not a validation
types vanish at compile time, so annotating the result of `JSON.parse`, `fetch`, or a database driver checks nothing. Validate untrusted data at runtime and let the annotation describe the already-checked result.
`any` in an annotation turns checking off, not up
writing `: any` to silence an error tells the compiler to stop reasoning about that value, and the looseness spreads to everything derived from it. Reach for `unknown` when you genuinely do not know the type.
Annotate exports even when inference works
an inferred return type on an exported function is part of your public API by accident. An explicit annotation pins the contract, so an internal change that breaks it fails in the file you edited.