TypeScript Types: Primitives, Arrays and Tuples

string, number, boolean, null and undefined, then arrays vs tuples, optional and named tuple elements, with the errors each one catches.

In this chapter

Primitives, arrays and tuples

More on Everyday types

Keep learning

Overview

chapters · everyday types Primitives, arrays and tuples An array is "any number of the same thing". A tuple is "exactly these things, in this order".

1. The problem: a point with one coordinate

A delivery app measures the distance between two map points. Each point should be two numbers: latitude and longitude. number[]: any length goes → NaN, no error number[] says "numbers", not "how many". [52.5] has one item, so lng1 is undefined at runtime, though typed number. 2.3 - undefined is NaN (Not a Number). a tuple: exactly two numbers Argument of type '[number]' is not assignable to parameter of type 'Point'. Source has 1 element(s) but target requires 2. A tuple is an array with a fixed length and a type per slot. lat: and lng: are just labels. [52.5] has 1 item but Point needs 2, so it does not fit.

2. The primitive types

Seven primitives (values that are not objects). With strict on (the tsconfig.json setting that turns on all safety checks), null and undefined are their own types. ints and floats share one type whole numbers beyond 2^53 deliberately empty missing / not set yet

strict mode keeps null out unless you allow it

Type 'null' is not assignable to type 'string'. ok: null is in the union

3. Lowercase, never the wrapper

String, Number and Boolean (capital letter) are JS wrapper objects: an object that holds a primitive, made by new String('x'). You almost never want them as types. String (object wrapper) Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. name: String allows a wrapper object. A plain string slot does not accept it, so line 2 fails. string (primitive) Same for number, boolean, bigint, symbol.

4. Arrays: T[] or Array<T>

Short form. Most codebases use it. Generic form (a type with a slot <T> you fill in). The exact same type. ['latte', 'mocha'] [4.5, 'latte'] (string | number)[] mixed values become a union string, or number[] missing parens! [] attaches to number only array of either What type does item get? string | undefined 1. menu is string[]: "strings, any number of them". 2. The type does not record the length, so TypeScript cannot know index 5 is past the end. 3. It assumes every read gives a string. At runtime item is undefined. Turn on noUncheckedIndexedAccess to get string | undefined. It is not part of strict.

5. Tuples: fixed length, typed slots

A tuple is an array whose length and the type of each position (slot) are fixed. ? marks a slot that may be left out. ... means "any number more". tuple type [string, number] ['latte', 4.5] two slots, in order [lat: number, lng: number] named: labels for docs and hovers only [string, number?] ['latte'] or ['latte', 2] [string, ...number[]] ['order', 4.5, 3, 2] rest: one string, then any numbers

the checker knows each slot

sku: string, qty: number, note: string | undefined Tuple type 'Line' of length '3' has no element at index '3'. Line 6 fails: Line has slots 0, 1 and 2 only (indexes start at 0). Unlike a plain array, a tuple knows its length, so index 3 is caught. returning an array literal

What as const does to an array literal

plain literal [52.5, 13.4] → number[] readonly [52.5, 13.4] mutating methods are gone p.push(1) ✕ Property 'push' does not exist Property 'push' does not exist on type 'readonly [number, number]'. Cannot assign to '0' because it is a read-only property. Line 2 fails: a readonly tuple has no push method, because push would change the length. Line 3 fails: slot 0 cannot be overwritten.

Remember

T[] and Array<T> are identical. Wrap unions in parens: (string | number)[]. Out-of-range array reads are typed as T, not T | undefined. An array literal is inferred as an array, never a tuple. many of one kind fixed slots [lat: number, lng: number] never changes ! Cheat code Lowercase primitives. Arrays for lists, tuples for fixed slots. Return a tuple? Annotate it, or TypeScript infers an array.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.