liveBook · Manning

Static typing

As explained in Essential TypeScript 5, Third Edition

A static type is a type checked for program values and expressions during compilation rather than only during execution.

Static typing makes assumptions about values explicit so the TypeScript compiler can report incompatible uses before the resulting JavaScript runs. TypeScript still compiles to JavaScript for a browser or Node.js; its type features constrain development without replacing the JavaScript runtime. section 7.2.1 section 1.1.1

Annotate a function

In src/index.ts, `function calculateTax(amount: number): number { return amount * 1.2; }` specifies both the accepted argument and the result. `calculateTax(12)` is accepted; passing `"Hello"` or `true` produces a compiler error because neither is a number. The colon introduces a type annotation, not a JavaScript runtime check. section 7.2.1

Annotations also apply to variables and class properties: `let price: number = 100` and `public id: number` state which values belong there. They are not required everywhere. From `let price = 100`, the compiler infers `number`; it can also follow the result of a calculation through later assignments. section 7.2.1 section 7.2.2

Catch a change in the result

If `calculateTax` returns `(amount * 1.2).toFixed(2)`, its inferred result becomes a string. A later `taxAmount / 2` then produces a compiler error. JavaScript could attempt to coerce that string during execution, but TypeScript flags the potentially problematic operation instead. section 7.2.2

Check an application boundary

A to-do collection can declare `addTodo(task: string): number`. Calling `collection.addTodo(todoItem)` with a `TodoItem` rather than task text produces TS2345: the argument is not assignable to `string`. Fix the call rather than weakening the parameter type merely to silence the error. section 2.2.5

Types can be combined with unions and narrowed with guards; `any`, `unknown`, and `never` address different cases where ordinary types are insufficient. Compiler settings also matter: `strictNullChecks` changes whether `null` and `undefined` are accepted for other types. Apply annotations where they clarify a contract, and let inference handle straightforward values. section 7.8 section 7.2.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through annotations and inference with compiler errors, then identifies unions, guards, special types, and configurable null checking.
Examples
Uses tax calculations and a to-do collection to expose wrong argument types and a string result used in arithmetic.
What it assumes
Assumes working JavaScript knowledge while explaining TypeScript annotations and inference through examples.
What you can do after
Apply it via TypeScript annotations and inference to detect incompatible values before running the compiled JavaScript.
How it connects
Connects type annotations and inference to function signatures, JavaScript coercion, type unions, type guards, and compiler options.

Also explained in

Compare how 12 books explain static typing →

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.