Dev Helpers

TypeScript / Optional and nullable values

Keep a valid zero score

Dev Helpers · September 15, 2025

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

Worked example

type Profile = {
  name: string;
  contact?: { email?: string };
  score: number | null;
};
const profiles: Profile[] = [
  { name: "Ada", score: 0 },
  { name: "Ben", contact: { email: "b@example.org" }, score: null },
];
console.log(profiles.map((p) => p.score ?? 10));

Result

[ 0, 10 ]

Implementation notes

A default should not convert an unknown score into a measured score accidentally. Preserve absence when the UI distinguishes missing data from a true zero.

Try it yourself

Compare a missing score, zero and a positive score.

Open the TypeScript example →

Editorial date September 15, 2025 · Published December 6, 2026

Related articles

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.