Dev Helpers

Developer journal

TypeScript

Type design, inference and the boundary between compile-time and runtime checks.

← All categories

TypeScript

Reject fractional cent amounts

A numeric type cannot by itself guarantee whole cents. Validate integrality where money enters the typed part of the application.

Infer route names from a registry

A route registry can drive both lookup and allowed route names. Type the keys from the registry so a removed route cannot survive in a separate union.

Infer a tuple element union

Indexing a tuple's type by number yields a union of its element types. Literal inference determines how precise that union becomes.

Create an object with a computed flag

A computed property name derives an object's key from a runtime value. Keep its allowed names narrow if downstream readers expect a fixed set.

Check for dangling dependency names

References between tasks can be well typed yet point to nonexistent names. Validate membership in the actual set of task IDs as a separate step.

Track touched fields

A touched-field map tracks interaction separately from the underlying values. A field can be touched and still contain its original value.

Read a typed property

A typed property read can couple a key to the shape of its record. That relationship catches misspellings before they become missing runtime values.

Project records into a Float32Array

A Float32Array stores numbers in single precision. Projecting records into it can change a decimal value even when the projected field was a number.

Pick two public fields

A public projection should explicitly copy the fields meant for its consumer. A narrower type annotation alone does not remove fields from a runtime object.

Require all partial settings

A partial input and a complete configuration serve different callers. Fill required values at one boundary so downstream code can rely on them.

Wrap a value in a typed array

A generic array wrapper can retain the type of the incoming value. That lets consumers reuse the helper without losing useful editor feedback.

Collect labels depth first

Depth-first traversal visits descendants before returning to a sibling. Declare the node shape recursively and decide how to handle empty child lists.

Check for another page

Pagination state needs a reliable signal for the final page. A missing cursor and an empty result are different conditions.

Create an initial state

An initial state object establishes which variants are available before the first event. Check how literal values are inferred when assigning it.

Exclude an internal field

Omit describes a narrower type at compile time. Removing an internal field from a payload also requires a runtime projection or destructuring step.

Extract sorted IDs

Extracting IDs and sorting them are separate operations. Check whether IDs need numeric comparison or lexical comparison.

Calculate a point magnitude

A coordinate type makes a numeric operation's inputs explicit. Magnitude combines dimensions into a single scalar, so the result loses direction.

Initialize a typed Set

A generic Set keeps insertions consistent across the lifetime of the collection. Explicit type arguments are especially useful when the set starts empty.

Derive a union from const data

A readonly literal list can be the source of truth for both runtime choices and a compile-time union. It avoids maintaining a duplicate list of allowed names.

Accept a single payload field

A narrow payload type describes the exact field a function needs. Keep that contract separate from the larger database record.

Represent numeric IDs as strings

Template literal types can restrict which strings are accepted as identifiers. Runtime data still needs validation when it crosses an input boundary.

Annotate an async return value

An async function returns a promise even when its body returns an immediate value. Annotating that contract makes consumers await the correct shape.

Extract all discriminant names

Discriminant fields tie runtime branches to a finite set of names. Extracting their union keeps routing or rendering logic aligned with actual variants.

Keep a valid zero score

Zero is a valid score even though it is falsy. A nullish default expresses absence without erasing an actual measurement.

Check a nullable input explicitly

An explicit null check preserves valid values that a broad truthiness check would reject. The remaining branch gains a narrower type.

Override a single option with satisfies

The satisfies operator checks a value against a contract without replacing its inferred type with that contract. It can catch a misspelled option early.