Dev Helpers

TypeScript / Typed collections

Initialize a typed Set

Dev Helpers · December 3, 2025

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

Worked example

const entries: readonly (readonly [string, number])[] = [
  ["api", 2],
  ["web", 4],
];
console.log([...new Set<number>([2, 3, 2])]);

Result

[ 2, 3 ]

Implementation notes

Objects in a Set compare by identity. A Set of typed record objects will still accept two distinct objects with equivalent fields.

Try it yourself

Add an allowed value, then try a value of another type.

Open the TypeScript example →

Editorial date December 3, 2025 · Published December 6, 2026

Related articles

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.

Reject fractional cent amounts

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