Skip to main content
ai-slop/redundant-type-coercionmanual fix

Redundant type coercion.

`String(x)`, `Number(x)`, or `Boolean(x)` applied to a parameter already typed as that primitive. The conversion repeats what the type system already guarantees.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

redundant-type-coercion.ts
✗ Flagged
export function formatPrice(amount: number, currency: string) {
  const value = Number(amount)
  return `${String(currency)} ${value.toFixed(2)}`
}
Why this trips

Deterministic AST match on TypeScript: flags `String()`, `Number()`, or `Boolean()` called on a parameter annotated `string`, `number`, or `boolean` respectively, within that function's own body.

redundant-type-coercion.ts
✓ Clean
export function formatPrice(amount: number, currency: string) {
  return `${currency} ${amount.toFixed(2)}`
}
What changed

Trust the typed boundary and drop the conversion, or validate unknown input once, where it enters the system.

Agents coerce inputs defensively in case a caller passes the wrong type, even when the signature rules that out.

How this rule is justified.

Rule id

ai-slop/redundant-type-coercion

Enforcing engine

ai-slop

Detector strategy

Deterministic AST match on TypeScript: flags `String()`, `Number()`, or `Boolean()` called on a parameter annotated `string`, `number`, or `boolean` respectively, within that function's own body.

Legitimate code that is NOT flagged

Coercion of `unknown` or untyped values, conversions between different primitives (`Number(s)` on a string), plain JavaScript files, and calls inside nested functions are not flagged.

Evidence

Part of aislop's defensive-pattern detectors for TypeScript, scored in the maintainability tier.