Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
A type guard must check every field that distinguishes the narrowed variant
Validate the complete shape at unknown boundaries; narrow trusted unions using sufficient discriminants.
Bad example
| 1 | function isShipped(value: unknown): value is ShippedOrder { |
| 2 | return typeof value === "object" && value !== null && "trackingNumber" in value; |
| 3 | } |
Explanation (EN)
A predicate over unknown data is unsound if it promises unchecked fields.
Objašnjenje (HR)
Predikat nad nepoznatim podacima nije pouzdan ako obećava neprovjerena polja.
Good example
| 1 | type ShippedOrder = { orderId: number; trackingNumber: string; carrier: string }; |
| 2 | function isShipped(value: unknown): value is ShippedOrder { |
| 3 | return typeof value === "object" && value !== null |
| 4 | && "orderId" in value && typeof value.orderId === "number" && Number.isSafeInteger(value.orderId) |
| 5 | && "trackingNumber" in value && typeof value.trackingNumber === "string" |
| 6 | && "carrier" in value && typeof value.carrier === "string"; |
| 7 | } |
Explanation (EN)
The unknown input is checked for every promised field; narrower domain constraints can be added where required.
Objašnjenje (HR)
Nepoznati ulaz provjerava se za svako obećano polje. Po potrebi dodaj stroža domenska ograničenja.
Notes (EN)
Distinguish narrowing an already validated union from validating unknown input. A runtime validator must establish every property promised by its result.
Exceptions / Tradeoffs (EN)
For a trusted union whose fields co-vary by definition, one distinguishing field can be sufficient even without a literal kind tag. Redundant checks do not repair an unvalidated boundary.
Iznimke / Tradeoffi (HR)
Za pouzdanu uniju čija su polja povezana definicijom jedno razlikovno polje može biti dovoljno i bez literalnog kind taga. Suvišne provjere ne popravljaju nevalidiranu granicu.