Instruction file imported from PaulJPhilp/wetware-software (
.cursor/rules/strict-typing-and-type-safety.mdc). Copyright stays with the author.
Strict Typing & Type Safety
Rule: Avoid any, enable strict mode in tsconfig.json, use readonly, explicit return types, and precise types where possible.
Why
- Catches bugs at compile time
- Improves developer confidence
- Enables more robust tooling support
Bad Example
function process(input: any): any {
return JSON.parse(input)
}
Using any loses type safety.
Good Example
interface Input { a: number; b: string }
function process(input: string): Input {
const obj = JSON.parse(input) as Input
return { a: obj.a, b: obj.b }
}
Or even better:
function process(input: string): Input {
return JSON.parse(input)
}
with correct type annotations and checks.
Tips
•In tsconfig.json, set "strict": true and related flags (noImplicitAny, strictNullChecks, etc.). •Use readonly for properties that should not change. •Explicitly annotate function return types, especially public APIs. •Prefer union / literal types / discriminated unions over any or unknown when possible.