Rules Hub
Code Ruleset
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP2stack specificStack: TypeScript
typescriptpropsapi-designclean-code
Prefer optional props over an explicit `| undefined` union
Choose optional syntax only when omission is allowed; required undefined-capable keys have different semantics.
PR: hegnar-components · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | interface Props { |
| 2 | text: string | undefined; |
| 3 | placeholder: string | undefined; |
| 4 | } |
Explanation (EN)
A required undefined-capable property forces callers to supply a key even when omission is intended.
Objašnjenje (HR)
Good example
New codetypescript
| 1 | interface Props { |
| 2 | text?: string; |
| 3 | placeholder?: string; |
| 4 | } |
| 5 | // undefined values then simply aren't passed as props |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Use an optional property when callers may omit the key; use a required T | undefined when presence itself is part of the contract.
Exceptions / Tradeoffs (EN)
With exactOptionalPropertyTypes, optional T does not automatically accept explicit undefined unless T includes it. Object spread and own-key consumers can observe presence.