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).
fullstack ruleP2universalStack: TypeScript
drytypescriptgenericstype-guards
Factor near-identical type guards into one generic validator
When several `isValidX` guards share the same body except the lookup set, extract a single generic helper and define each guard as a thin call to it.
PR: hegnar-ws · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | const isValidArticleSource = (v: unknown): v is ArticleSource => |
| 2 | typeof v === 'string' && Object.values(ARTICLE_SOURCES).includes(v as ArticleSource); |
| 3 | const isValidAptomaType = (v: unknown): v is AptomaType => |
| 4 | typeof v === 'string' && Object.values(APTOMA_TYPES).includes(v as AptomaType); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | type ValueOf<T> = T[keyof T]; |
| 2 | const isValidValue = <T extends object>(v: unknown, set: T): v is ValueOf<T> => |
| 3 | typeof v === 'string' && Object.values(set).includes(v as ValueOf<T>); |
| 4 | const isValidArticleSource = (v: unknown): v is ArticleSource => isValidValue(v, ARTICLE_SOURCES); |
Explanation (EN)
Objašnjenje (HR)