Patterns / Type escape hatches
Type escape hatches
An agent fighting a type error has two options: fix the types or silence the checker. Silencing is shorter, so it wins. The PR compiles, and the type system no longer protects that code.
What it looks like
const data = rows as any; return value as unknown as User; // @ts-ignore // eslint-disable-next-line x = foo() # type: ignore
Why it matters
Each cast is a spot where a wrong shape flows through unchecked. They also spread: an `any` return type infects every caller.
Before and after
Before
const user = (await res.json()) as any; return user.profile.name;
After
const user = UserSchema.parse(await res.json()); return user.profile.name;
How to fix it
- Validate external data (zod, valibot, pydantic) instead of casting.
- If a suppression is really needed, give the reason: // @ts-expect-error upstream types miss `cause` (lib#123). Suppressions with a reason aren't flagged.
When it's fine
Narrow, documented suppressions for upstream type bugs.
Catch this automatically. SlopScore for Code checks every pull request for this pattern (rule
unsafe-cast) and 11 others. Score a public PR or add the free GitHub Action.