Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Filter out falsy entries before mapping to extract ids
When extracting a list of ids from records that may lack one, filter for the presence of the field first, then map, so the resulting array has no undefined entries.
Bad example
| 1 | const recordIds = values.recordsList.map(el => el.id); // may include undefined |
Explanation (EN)
Mapping an optional ID can produce undefined entries.
Objašnjenje (HR)
Good example
| 1 | const recordIds = values.recordsList |
| 2 | .map(record => record.id) |
| 3 | .filter((id): id is number => id != null); |
Explanation (EN)
For a number | null | undefined source, this keeps every supplied number including zero; domain range validation is separate.
Objašnjenje (HR)
Notes (EN)
Exclude only genuinely missing or invalid IDs before passing the list to a consumer that requires valid identifiers.
Exceptions / Tradeoffs (EN)
A truthiness filter drops valid zero or empty-string IDs if the domain allows them. Filtering source objects does not always narrow the later property type; use an appropriate predicate/result type.