Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Avoid mixing optional and nullable types
Do not combine optional syntax (?) with null types in TypeScript definitions, as it creates ambiguous and redundant 'missing' states.
Bad example
| 1 | type ArticleProps = { |
| 2 | // Ambiguous: Can be undefined (missing prop), null (explicitly no data), or the object. |
| 3 | ticker?: Ticker | null; |
| 4 | }; |
| 5 |
|
| 6 | const getLabel = (ticker?: Ticker | null) => { |
| 7 | if (!ticker) return null; |
| 8 | return ticker.symbol; |
| 9 | }; |
Explanation (EN)
Using both `?` (undefined) and `| null` creates two ways to represent 'no value'. This forces the consumer to handle both `undefined` and `null`, implies a semantic difference that rarely exists, and makes the type definition confusing.
Objašnjenje (HR)
Korištenje `?` (undefined) i `| null` istovremeno stvara dva načina za predstavljanje 'nepostojeće vrijednosti'. To prisiljava onoga tko koristi kod da obrađuje i `undefined` i `null`, implicira semantičku razliku koja rijetko postoji i čini definiciju tipa zbunjujućom.
Good example
| 1 | type ArticleProps = { |
| 2 | // Clear: The value is explicitly nullable (e.g., from API response). |
| 3 | ticker: Ticker | null; |
| 4 | }; |
| 5 |
|
| 6 | // OR if it is truly just an optional configuration: |
| 7 | // ticker?: Ticker; |
| 8 |
|
| 9 | const getLabel = (ticker: Ticker | null) => { |
| 10 | if (!ticker) return null; |
| 11 | return ticker.symbol; |
| 12 | }; |
Explanation (EN)
Stick to a single strategy for missing values. If the data comes from an API (which often returns null), use `| null`. If it is an optional configuration prop, use `?`. Do not mix them.
Objašnjenje (HR)
Drži se jedne strategije za nedostajuće vrijednosti. Ako podaci dolaze s API-ja (koji često vraća null), koristi `| null`. Ako je riječ o opcionalnom prop-u za konfiguraciju, koristi `?`. Nemoj ih miješati.