A type hint, or type annotation, makes an expected type explicit so the TypeScript compiler can check how a value is used. Write a colon and a type after a declared name: `let price: number = 100`. The same syntax applies to properties, parameters, and variables; a return type follows a function’s parameter list. section 7.2.1 section 2.2.5
Catch a mismatched argument
In `src/index.ts`, `function calculateTax(amount: number): number { return amount * 1.2; }` declares both its input and result as numbers. `calculateTax(12)` is accepted, but passing `"Hello"` or `true` produces a compiler error because neither argument matches `number`. section 7.2.1
Annotate where inference does not express the intended constraint. `let prices: number[] = [100, 75, 42]` restricts an array’s elements without changing JavaScript arrays’ flexible length. An empty array gives the compiler no initial element types to use: the shown configuration infers `any[]`, leaving a gap in checking, while the shown `strictNullChecks` configuration infers `never` and rejects additions. section 9.2 section 9.2.3
Annotations also express intent that an initializer may not convey. An array inferred from two subclasses can exclude a third subclass despite their common superclass; annotating it with the superclass array type permits all three. For function parameters, put an annotation before a default value, after `?` for an optional parameter, and use an array type for a rest parameter. section 11.3.7 section 8.2.2
A `number` annotation alone does not exclude `null` under the shown default settings. Enable `strictNullChecks` in `tsconfig.json` to reject that call, or declare `number | null` and handle the null case. Annotations work alongside inferred types; not every declaration needs one. section 8.2.2 section 7.8
















