liveBook · Manning

Data type

As explained in Essential TypeScript 5, Third Edition

A data type is a classification that determines the kinds of values a program can represent and the operations applicable to them.

A data type classifies a value and determines how it can be used. JavaScript provides built-in types such as number, string, boolean, symbol, null, undefined, and object. Values have types rather than variables, so a variable can change type when assigned a different value. section 7.2 section 3.7

A variable changing type

let myVar; myVar = 12; myVar = "Hello"; myVar = true; console.log(`${myVar} = ${typeof myVar}`); section 7.2

JavaScript infers a value's type and dynamically accepts different argument types. That flexibility can hide incorrect assumptions: a calculation intended for numbers produces a numeric result for 12, NaN for "Hello", and 1.2 for true after coercion. TypeScript adds annotations, inference, unions, assertions, guards, and compiler settings to control which values and operations are allowed. section 7.2 section 7.0

Table 7.1 Chapter summary (view table figure)

Problem

Solution

Listing

Specify a type

Use a type annotation or allow the compiler to infer a type

10–13

Inspect the types that the compiler infers

Enable the declarations compiler option and inspect the compiled code

14, 15

Allow any type to be used

Specify the any or unknown types

16–19, 29, 30

Prevent the compiler from inferring the any type

Enable the noImplicityAny compiler option

20

Combine types

Use a type union

21, 22

Override the type expected by the compiler

Use a type assertion

23–25

Test for a primitive value type

Use the typeof operator as a type guard

26–28

Prevent null or undefined from being accepted as values of other types

Enable the strictNullChecks compiler option

31–33

Override the compiler to remove null values from a union

Use a non-null assertion or use a type guard

34, 35

Allow a variable to be used when it has not been assigned a value

Use the definite assignment assertion

36, 37

Use the reference table to navigate the main TypeScript decisions for specifying, inspecting, broadening, combining, narrowing, and null-checking types. section 7.0

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains built-in types, runtime inspection, dynamic coercion, and TypeScript controls, but does not detail advanced type-system internals.
Examples
Demonstrates reassignment, typeof output, built-in type descriptions, and a calculateTax function receiving numbers, strings, and booleans.
What it assumes
Assumes JavaScript familiarity while teaching built-in types, dynamic assignment, inference, and the motivation for TypeScript's static checks.
What you can do after
Diagnose unexpected results by inspecting runtime types and apply TypeScript features to restrict or narrow permitted values.
How it connects
Connects data types with static types, type inference, type annotations, type unions, type assertions, type guards, and type coercion.

Also explained in

Compare how 18 books explain data type →

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.