More in TypeScript & Web APIs — page 13
TypeScript Function Types: Expressions vs. Signatures
TypeScript describes function shapes with type expressions or call signatures. Use a simple expression like `(s: string) => void` for callbacks. For functions with properties, use a call signature. The footgun: parameter names are required in type expressions.
TypeScript Interfaces: Naming the Shape of Your Data
An interface is a contract for an object's shape, caring what properties it has, not its class. Use it to define function parameters or API responses. The footgun: TypeScript allows objects with extra properties, checking only for the required ones.
Union Types: When a Value Can Be One of Several Things
A Union Type is an 'OR' for your types, letting a value be one of several options, like `string | number`. Use it for flexible function arguments or varied API responses. The footgun: you can only use properties common to all types until you narrow.
TypeScript Type Inference: How It Knows Without Being Told
TypeScript's type inference deduces types so you don't have to annotate everything. It infers types from variable initializations and function returns. The main footgun is its 'best common type' for arrays, which can create an overly specific union type.
TypeScript's Basic Types: The Building Blocks
TypeScript builds on JavaScript's primitives (`string`, `number`, `boolean`) by letting you explicitly declare a variable's type. This is the foundation for catching errors early. The main footgun is confusing tuples `[string, number]` with flexible arrays.
TypeScript Type Annotations: Defining Your Data's Shape
Type annotations are contracts for your data, telling TypeScript what to expect from variables and functions. You'll use them for primitives like `string` or `number`, and for arrays like `string[]`.