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

















