Rules Hub
Coding Rules Library
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.
Bad example
| 1 | type 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
| 1 | type 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).