asserticide
v0.4.0
Published
Kill every redundant type assertion in your TypeScript codebase
Downloads
340
Maintainers
Readme
asserticide
Kill every redundant type assertion and non-null assertion in your TypeScript codebase
How
For each assertion.
- Delete it.
- Typecheck via
tsgo. - Keep the deletion if it typechecks; revert otherwise.
What survives is the set of assertions actually doing work — plus the small set preserved by rule.
Preserved by rule
A few cases where an assertion is kept even though tsgo would accept the deletion:
as const— never touched.as never— never touched; almost always an intentional type hack.x as Twhen removing it would letanyfromxsilently propagate (andT≠any).x as any as T— neither half is removed in a way that would change the value's effective type.- An assertion whose removal would change a function's inferred return type (explicit annotations are exempt).
{ ... } as Tinitializing an unannotated variable — the assertion drives the object literal's contextual typing.x!whenstrictNullChecksis off — usually scaffolding for an in-progress migration to strict null checks.
Use
npx asserticide # uses ./tsconfig.json
npx asserticide path/to/tsconfig.json
npx asserticide path/to/dir # uses path/to/dir/tsconfig.jsonRequires Node ≥ 24. asserticide refuses to run if the project doesn't typecheck cleanly under tsgo. Commit your working tree first so you can review the diff afterwards.
Sample output:
---
total assertions found: 412
removed assertions: 287
reverted by typecheck: 118
preserved by rule: 7
files changed: 54
- src/api/client.ts
- src/components/Grid.tsx
- ...How it compares to no-unnecessary-type-assertion
The difference is scale.
The @typescript-eslint/no-unnecessary-type-assertion lint rule works at the expression level: it reports an assertion when removing it preserves the expression type or still satisfies the contextual type at that position.
asserticide works at project level: it tries deleting each type assertion, runs tsgo, and keeps the edit if the checked project still typechecks and no preservation rule applies.
So asserticide can remove assertions with real local type effects when those effects are unused projectwide.
