TypeScript · TS0008
Explain necessary type assertions
Require a nearby SAFETY comment for non-const TypeScript assertions, except Vue's `X as PropType<T>` prop declarations.
typescript/strict/require-safety-comment-for-type-assertionsrc/rule-packs/typescript/rules/require-safety-comment-for-type-assertion.ts Upstream docs Run this rule
pnpm vite-doctor . --extends auto,typescript/strict --rules typescript/strict/require-safety-comment-for-type-assertion
Why it matters
A necessary assertion depends on an invariant TypeScript cannot express. Recording that invariant lets reviewers verify the claim and detect when later changes invalidate it.
Recommended fix
Remove the assertion, or add a specific SAFETY comment immediately before the containing statement, or before the line of the ternary, &&, ||, or else branch that holds it.
Useful links
Example
State the checked invariant
Before
const userId = value as UserId
After
// SAFETY: parseUserId validated the identifier before branding it.
const userId = value as UserId