liveBook · Manning

Type hints

As explained in Essential TypeScript 5, Third Edition

A type annotation is syntax that explicitly associates a declared program element with a type for compiler checking.

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

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Shows annotation syntax across declarations, functions, and arrays, then examines inference and configuration pitfalls rather than runtime enforcement.
Examples
Uses a tax function’s rejected arguments, typed price arrays, empty-array errors, and a superclass-typed array.
What it assumes
Assumes familiarity with JavaScript declarations, functions, and arrays while explaining TypeScript annotation syntax directly.
What you can do after
Apply type annotations in TypeScript to constrain values and resolve misleading inference or compiler errors.
How it connects
Links type inference, static typing, array types, function parameters, class inheritance, type unions, and strict null checking.

Also explained in

Compare how 17 books explain type hints →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.