Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Type safety practices including exhaustive checks, explicit return types, and Result-based error handling. Use when writing TypeScript logic that handles multiple cases or error conditions.
.claude/skills/trezor-defensive-programming/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-02 | ✗→✓ | ▲ Improved | 33% | 0% |
| case-05 | ✗→✓ | ▲ Improved | -12% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 52% | 0% |
| case-10 | ✗→✓ | ▲ Improved | -11% | 0% |
| case-11 | ✗→✓ | ▲ Improved | 43% | 0% |
Whenever possible, cover all cases. If a new case is added in the future, TypeScript should force the developer to set behavior for it.
Makes sure all cases are covered in a function.
ts// TS Error: Function lacks ending return statement and return type does not include 'undefined' export const isEnabled = (status: 'a' | 'b' | 'c'): boolean => { if (status === 'a') { return true; } if (status === 'b') { return false; } };
exhaustive switchMakes sure all cases are covered in a switch statement.
ts// TS Error: Argument of type '"c"' is not assignable to parameter of type 'never' export const isEnabled = (status: 'a' | 'b' | 'c') => { switch (status) { case 'a': return true; case 'b': return false; default: return exhaustive(status); } };
Alternative to an exhaustive switch statement.
tstype Schema = { a: number; b: number; }; // TS Error: Property 'b' is missing in type '{ a: () => string; }' but required in type '{ a: () => void; b: () => void; }'. const result: { [K in keyof Schema]: () => void } = { a: () => console.log('This is A'), };
Unless failures are unpredictable, pass errors via return and do not throw. Throwing exceptions is not type-safe. There is a Result type that shall be used.
Bad:
tstry { const result = await action(); } catch (error) { // Possible errors cannot be typed // ... }
Good:
tsconst result = await action(); if (result.error) { const { type } = result.error; switch (type) { case 'ErrorA': // ... do stuff case 'ErrorB': // ... do different stuff default: return exhaustive(type); } }
Other measured skills in the registry, with their headline benchmark lift.