| name | imprecise-over-inaccurate |
| description | Use when types become too complex. Use when precision causes false positives. Use when accuracy is uncertain. |
Prefer Imprecise Types to Inaccurate Types
Overview
An imprecise type that's correct is better than a precise type that's wrong.
When you make types more precise, you risk making them inaccurate. Inaccurate types cause false positives (valid code rejected) or false negatives (invalid code accepted). Both erode trust in the type system.
When to Use This Skill
- Making types "more precise"
- Getting unexpected type errors
- Types that reject valid use cases
- Debugging complex generic types
The Iron Rule
Types should never reject valid code.
When in doubt, be less precise.
Remember:
- False positives (rejecting valid code) erode trust
- False negatives (accepting invalid code) cause bugs
- Imprecise but correct > precise but wrong
- Complex types are harder to debug
Detection: Over-Precise Types
type CSSColor =
| 'red' | 'blue' | 'green' | 'yellow'
| `#${string}`
| `rgb(${number}, ${number}, ${number})`;
function setColor(color: CSSColor) { }
setColor('rgb(255,255,255)');
setColor('rgba(0, 0, 0, 0.5)');
The precise type is inaccurate - it rejects valid CSS.
Better: Imprecise but Accurate
function setColor(color: string) { }
setColor('rgb(255, 255, 255)');
setColor('rgba(0, 0, 0, 0.5)');
setColor('invalid');
The string type is imprecise (accepts invalid colors) but accurate (never rejects valid colors).
Middle Ground: Partial Precision
type CSSColor =
| 'red' | 'blue' | 'green' | 'yellow'
| (string & {});
setColor('red');
setColor('custom');
Real Example: JSON Schema
interface JSONSchema {
type: 'string' | 'number' | 'object' | 'array' | 'boolean' | 'null';
properties?: Record<string, JSONSchema>;
items?: JSONSchema;
}
const schema: JSONSchema = {
type: ['string', 'null'],
};
The "precise" type is inaccurate. A simpler type might be better:
interface JSONSchema {
type?: string | string[];
[key: string]: unknown;
}
When Precision Pays Off
Precision is worth it when:
- You control all the inputs (internal types)
- The domain is well-defined (finite, known values)
- Errors are caught early (development time)
- The type is simple enough to be obviously correct
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
type PositiveInteger = number;
When to Back Off
Back off from precision when:
- External data is involved (APIs, user input)
- The domain is complex (CSS, HTML, regex)
- The type rejects valid use cases
- The type is complex and hard to verify
type CSSLength = `${number}px` | `${number}em` | `${number}rem`;
type CSSLength = string;
type CSSLength = string;
False Positives vs False Negatives
| Problem | Impact | Solution |
|---|
| False Positive | Valid code rejected | Make type less precise |
| False Negative | Invalid code accepted | Validate at runtime |
False positives are worse because they:
- Block legitimate work
- Require workarounds (
as any)
- Erode trust in TypeScript
Progressive Refinement
Start imprecise, add precision as needed:
interface Config {
[key: string]: unknown;
}
interface Config {
port?: number;
host?: string;
[key: string]: unknown;
}
interface Config {
port: number;
host: string;
}
Pressure Resistance Protocol
1. "More Precise is Better"
Pressure: "Types should be as specific as possible"
Response: Only if they're also accurate. Precise + wrong = worse.
Action: Check: does this precision reject valid code?
2. "We Should Catch All Errors"
Pressure: "The type should prevent all invalid values"
Response: You can't type-check everything. Runtime validation exists.
Action: Type what you can accurately. Validate the rest at runtime.
Red Flags - STOP and Reconsider
- Complex types with many unions/intersections
- Type errors on code you know is valid
- Frequent use of
as any to work around types
- Types that are hard to explain
Common Rationalizations (All Invalid)
| Excuse | Reality |
|---|
| "The type is technically correct" | If it rejects valid code, it's not correct |
| "Users should write better code" | Types should match reality, not ideals |
| "We can add exceptions" | Exceptions mean the type is wrong |
Quick Reference
type Color = 'red' | 'blue' | `#${string}`;
setColor('rgb(0,0,0)');
type Color = string;
setColor('rgb(0,0,0)');
type Color = 'red' | 'blue' | (string & {});
setColor('red');
setColor('rgb(0,0,0)');
The Bottom Line
Accuracy trumps precision.
A type that accepts some invalid values is better than a type that rejects valid values. When making types more precise, verify they remain accurate. If precision causes false positives, back off.
Reference
Based on "Effective TypeScript" by Dan Vanderkam, Item 40: Prefer Imprecise Types to Inaccurate Types.