Command Palette

Search for a command to run...

Hectal
Phase 5Beginner6 of 18 in TypeScript

Arrays, Tuples, Enums and Special Types

Read-only arrays, tuples, enums and their alternatives, object vs {} vs unknown, the never type, and type assertions with satisfies.

You know how to describe single values, objects and unions. This phase fills in the rest of the everyday toolbox: lists that must not change (readonly arrays), lists with a fixed shape (tuples), named constants (enums), and the special types that sit at the very top and very bottom of TypeScript's type system.

You'll also learn the tools for telling the compiler something it can't work out by itself: type assertions (as, !) and the newer satisfies operator, and when each one hides a bug instead of fixing it.

Everything here is checked by the real compiler in your browser. Read each error before you scroll: predicting the message is the quickest way to learn how the checker thinks.

0/6 · 0%
6 topics ~3 h 20 code blocks & diagrams
Start with the first topic
1
5.1

Readonly Arrays and Immutability

A readonly T[] is a list you can look at but not change: no push, no assigning to an index. It is a promise the checker enforces, and it is shallow and gone at runtime.

27 min 4 code practice

2
5.2

Tuples

A tuple is an array whose length and the type at each position are fixed, like [string, number] for a name and an age. Functions use tuples to return several values at once, the way useState does.

27 min 3 code practice

3
5.3

Enums

An enum gives names to a fixed set of related values, like Direction.Up. Unlike almost everything else in TypeScript, an enum creates a real object at runtime, which is why many teams prefer a union of literals or an as const object.

27 min 4 code practice

4
5.4

object, Object, {} and the Top Types

TypeScript has several types that mean "almost anything": unknown, {}, object, Object and any. They differ in exactly what they let in and what they let you do, and picking the wrong one is a classic source of bugs.

27 min 3 code practice

5
5.5

never in Depth

never is the type with no values at all. It is what a function that never returns gives back, what a check leaves when every possibility has been ruled out, and the way the compiler tells you "this should be impossible".

27 min 3 code practice

6
5.6

Type Assertions and the satisfies Operator

A type assertion (as, !) tells the compiler "trust me, I know the type" and the compiler stops checking; satisfies does the opposite and checks a value against a type without changing what TypeScript knows about it.

27 min 3 code practice

Practise this phase

Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.