liveBook · Manning

Type system

As explained in Essential TypeScript 5, Third Edition

The JavaScript type system is the set of rules governing the types and type-related behavior of JavaScript values and expressions.

The JavaScript type system governs how values behave as numbers, strings, booleans, objects, and other built-in types. JavaScript assigns types to values rather than variables, so one variable can hold values of different types over time. TypeScript builds on this behavior by adding static type checking, making code more predictable before it runs. section 3.3 section 3.3.1 section 7.8

The core rules include:

  • Primitive types include number, string, boolean, symbol, null, and undefined; object represents compound values. section 3.3
  • JavaScript can coerce values during comparisons and operations, so a number compared with a numeric string may match while addition concatenates them. section 3.2
  • The typeof operator reports a value's type, although typeof null returns object because of longstanding language behavior. section 3.3.1
  • TypeScript restricts JavaScript's dynamic behavior with annotations, inference, unions, assertions, guards, and special types such as any, unknown, and never. section 7.8

Mixed values can produce surprising results

let hatPrice = 100; let bootsPrice = "100"; if (hatPrice == bootsPrice) { console.log("Prices are the same"); } let totalPrice = hatPrice + bootsPrice; console.log(totalPrice); // 100100 section 3.2

Table 3.1 The JavaScript built-in types (view table figure)

Name

Description

number

This type is used to represent numeric values. Unlike other programming languages, JavaScript doesn’t differentiate between integer and floating-point values, both of which can be represented using this type.

string

This type is used to represent text data.

boolean

This type can have true and false values.

symbol

This type is used to represent unique constant values, such as keys in collections.

null

This type can be assigned only the value null and is used to indicate a nonexistent or invalid reference.

undefined

This type is used when a variable has been defined but has not been assigned a value.

object

This type is used to represent compound values, formed from individual properties and values.

Use the built-in-types table to compare primitive types with object values and review each type's role. section 3.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines built-in types, coercion, typeof, and dynamic reassignment, then introduces TypeScript checking without covering deeper advanced features.
Examples
Uses mixed price values, typeof reassignment, and a built-in-types table to demonstrate runtime behavior.
What it assumes
Assumes JavaScript familiarity and teaches its basic types and runtime behavior before relying on TypeScript.
What you can do after
Diagnose/debug with it by recognizing coercion, inspecting values with typeof, and choosing TypeScript checks for safer assignments.
How it connects
Connects values, primitive types, objects, type coercion, typeof, static types, type inference, unions, and type guards.

Also explained in

Compare how 6 books explain type system →

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.