Rules Hub
Coding Rules Library
← 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 ruleP1universalStack: TypeScript
typescriptdiscriminated-uniontype-safety
Use a discriminated union so switch branches narrow without casts
Model column/field configs as a discriminated union keyed on `type` so each switch case infers the correct value type instead of using `as number`.
PR: hegnar-forum-web · org-mining-3rd-2026-06Created: Jun 18, 2026
Bad example
Old codetsx
| 1 | switch (column.type) { |
| 2 | case 'number': |
| 3 | return <Cell value={value as number} />; |
| 4 | case 'percentage': |
| 5 | return <Cell value={value as number} showPercentage />; |
| 6 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | type ColumnDef = |
| 2 | | { type: 'number'; field: NumberField } |
| 3 | | { type: 'percentage'; field: PercentageField }; |
| 4 |
|
| 5 | switch (column.type) { |
| 6 | case 'number': |
| 7 | return <Cell value={row[column.field]} />; // narrows to number |
| 8 | } |
Explanation (EN)
Objašnjenje (HR)