Skip to content

Chapter 2 of 8

Basic Types & Type System Fundamentals

TypeScript's type system starts with a small set of primitive types: number, string, boolean, null, undefined, symbol, and bigint. These primitives serve as the building blocks for typing variables, function parameters, and return values. To annotate a variable's type explicitly, you place a colon and the type after the variable name, as in let name: string = "Alice";. Explicit annotations improve readability and prevent unintended type mismatches.

TypeScript also performs type inference: when a variable is initialized with a value, the compiler automatically infers its type from that initializer. Writing let count = 5; causes TypeScript to treat count as a number, eliminating the need for redundant annotations while still preserving type safety. Inference reduces boilerplate, but explicit annotations remain valuable wherever clarity or contract enforcement matters.

The type system includes a few special escape-hatch types. The any type disables type checking entirely, letting a value be treated as if it could be any type; it should be used sparingly because it bypasses the safety TypeScript exists to provide. The unknown type is a safer top type, requiring a type check or narrowing before any operation is performed on the value, which prevents accidental misuse. Finally, the never type represents values that never occur, such as the result of a function that always throws or the unreachable branch of an exhaustive switch statement.

All chapters
  1. 1Introduction to TypeScript & Setup
  2. 2Basic Types & Type System Fundamentals
  3. 3Functions & Collection Types
  4. 4Interfaces, Type Aliases & Object Shapes
  5. 5Classes & Object-Oriented Programming
  6. 6Generics for Reusable Code
  7. 7Modules & Advanced Type-Level Features
  8. 8Strict Mode & Interoperability with JavaScript

Drill it

Reading is not remembering. These come from the Typescript Essentials deck:

Q

What is TypeScript?

TypeScript is a statically typed superset of JavaScript developed by Microsoft that compiles to plain JavaScript code. It adds optional static typing, interface...

Q

How does TypeScript relate to JavaScript?

TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript. TypeScript code is transpiled to JavaScript, allowing it to...

Q

How do you install TypeScript?

Install TypeScript globally using npm install -g typescript or locally as a dev dependency with npm install typescript --save-dev. Verify installation with tsc...

Q

What is the TypeScript compiler command?

The TypeScript compiler is invoked with tsc. Use tsc file.ts to compile a single file or tsc to compile based on tsconfig.json.