Why this matters
Precise handling reduces brittle branches and unsafe casts.
Define a discriminated union or error class hierarchy and use user-defined type guards before handling specific cases.
Precise handling reduces brittle branches and unsafe casts.
Side-by-side examples engineers can pattern-match during review.
catch(e){ if((e as any).status) retry(); }type HttpError={kind:'http',status:number,body?:unknown}; const isHttp=(x:unknown):x is HttpError=>!!x && (x as any).kind==='http';if ((e as any).status) {}function isHttp(x:unknown): x is HttpError { /*...*/ }From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.