Rules Hub

Coding Rules Library

← Back to all rules
fullstack ruleStack: typescript
typescripttypesreadabilityclean-code

Prefer explicit boolean literals for discriminated unions

Use strict `true` and `false` literals instead of `undefined` for boolean discriminants in unions to improve clarity and type safety.

PR: Feat/FCK-2245 - Cache Bellsheep and profile loaders with TanStack Query #343Created: Dec 10, 2025

Bad example

Old codets
1type ItemProps =
2 | { isLegacy: true; legacyId: string }
3 | { isLegacy: undefined; modernId: string }; // Ambiguous discriminant

Explanation (EN)

Using `undefined` as a discriminant implies a missing value rather than a specific state. It makes the union harder to reason about and type-check strictly.

Objašnjenje (HR)

Korištenje `undefined` kao diskriminatora implicira nedostajuću vrijednost umjesto specifičnog stanja. To otežava razumijevanje unije i strogu provjeru tipova.

Good example

New codets
1type ItemProps =
2 | { isLegacy: true; legacyId: string }
3 | { isLegacy: false; modernId: string }; // Clear boolean discriminant

Explanation (EN)

Using explicit `true` and `false` literals clearly demarcates the branches of the union. It documents intent precisely: the value is known and it is false.

Objašnjenje (HR)

Korištenje eksplicitnih `true` i `false` literala jasno razgraničava grane unije. Precizno dokumentira namjeru: vrijednost je poznata i ona je netočna (false).