Types & annotations
23 steps in 7 sets of TypeScript.
The type system as you meet it. Primitives and literal types, then the awkward ones, any and unknown and never, which are worth understanding early because the difference between them is the difference between types that help and types that lie.
Then narrowing, objects and interfaces, functions and tuples, and finally satisfies and the strictness flags. Twenty-three steps, every one typechecked under strict.
Primitives & literals
- Primitive typesAnnotating the basic types, arrays, and tuples.
- Literal and union typesA literal type is one exact value; a union is a closed set of them.
- Enums and const assertionsFixed sets of values, as enums or literal unions.
- Enums and the alternativeWhat an enum gives you, and why a const object often beats it.
The awkward types
- unknown, any and neverunknown demands a check, any waives them, never means it cannot happen.
- Assertions and their costas, non-null, and the two escape hatches you should distrust.
Narrowing
- Narrowing and type guardsTeaching the compiler what a value actually is.
- NarrowingThe compiler follows your checks: typeof, in, instanceof, truthiness.
- Type predicatesA function returning
x is Tteaches the compiler what you checked. - Exhaustive switchThe never trick that makes the compiler reject an unhandled case.
Objects & interfaces
- Interfaces and type aliasesDescribing object shapes, and combining them.
- Object typesOptional, readonly, index signatures, and excess property checks.
- type against interfaceInterfaces merge and are implemented; type aliases compose.
- Structural typingCompatibility is about shape, not about names or ancestry.
- readonly and immutabilityReadonly arrays and objects, and where the guarantee stops.
Functions & tuples
- Typed functionsParameter, return, and function-type annotations.
- Function types and overloadsA call signature as a type, optional and default parameters, overloads.
- TuplesFixed-length arrays with a type per position, and labeled elements.
- Clamp and lerpClamp and linear interpolation, the two functions every animation needs.
- Range arrayBuilds an array of numbers, the eager counterpart to a generator.
satisfies & strictness
- satisfiesCheck a value against a type without widening it to that type.
- What strict turns onThe individual flags behind strict, shown by what each one catches.
Encore
- validate-config.tsParse unknown input into a fully typed config or issues.