Skip to content

Require Safety Comment for Type Assertion

Error
small-rules/require-safety-comment-for-type-assertion

Require a nearby SAFETY comment (or a described Oxlint suppression of typescript/no-unsafe-type-assertion) for every TypeScript type assertion except const assertions.

Rationale

Ported from dmmulroy/anti-slop. A cast is an unchecked claim. A nearby SAFETY: comment records the invariant that makes that claim legitimate; as const is exempt because it does not claim an unrelated runtime shape.

Diagnostic Messages

missingSafetyComment
This type assertion has no `SAFETY:` justification. State the checked invariant immediately before the assertion or its containing statement, or add a described `oxlint-disable-next-line typescript/no-unsafe-type-assertion` directive.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/require-safety-comment-for-type-assertion": "error"
}
}

Examples

unexplained type assertion
const userId = value as UserId;
assertion with checked invariant
// SAFETY: parseUserId validated the identifier before branding it.
const userId = value as UserId;