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).
backend ruleP2stack specificStack: TypeScript
typescriptormtyping
Type an ORM schema/model with its generic instead of casting
Pass the domain type as the schema/model's type argument (e.g. `new Schema<Applicant>()`) so queries return typed results, instead of returning loose objects and sprinkling `as T` casts at the call sites. The generic gives real type-checking; the cast only silences the compiler.
PR: profico-org-corpus batch2 (all stacks/products)Created: Jul 26, 2026
Bad example
Old codets
| 1 | const schema = new Schema({ name: String }); |
| 2 | const doc = (await Model.findById(id)) as Applicant; |
Explanation (EN)
The untyped schema forces an `as Applicant` cast that isn't actually checked.
Objašnjenje (HR)
Good example
New codets
| 1 | const schema = new Schema<Applicant>({ name: String }); |
| 2 | const doc = await Model.findById(id); // already Applicant | null |
Explanation (EN)
The generic makes results typed, so no cast is needed.
Objašnjenje (HR)