Skip to content

No Widen Then Assert

Error
small-rules/no-widen-then-assert

Disallow local const flows that explicitly widen a known value before asserting the widened binding to a narrower type.

Rationale

Ported from dmmulroy/anti-slop. Widening a known local value then asserting it back recreates evidence that was already available. Preserve the precise type through the flow, or parse external data once.

Diagnostic Messages

widenThenAssert
Binding "{{name}}" discards type evidence and later recreates it with an assertion. Keep the precise type from initialization through use; parse boundary input once.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-widen-then-assert": "error"
}
}

Examples

widened binding asserted back
const source = { id: 'second' };
const widened: unknown = source;
const parsed = widened as { readonly id: string };
boundary input with a stated invariant
// SAFETY: parseUser validated this payload.
declare const input: unknown;
const parsed = input as { readonly id: string };