Problem: TypeScript optional fields reduce type safety


Problem: TypeScript optional fields reduce type safety. Why? Because an optional field can't specify when the field is optional vs required.

Solution: Consider a discriminated union instead.

This makes the differences between types explicit, so TypeScript can enforce it.

Oops, last line should be:

type Shape = Circle | Rectangle;

Corrected:

View original on X