Skip to main content

Avoid as Type Assertions

Avoid TypeScript as type assertions

Source: .agents/rules/no-type-assertion-as.mdc

Metadata

  • alwaysApply: true

Content

Avoid as Type Assertions

  • Do not use as for type assertions in new or updated code.
  • Prefer precise type definitions at the source (API/service types, prop types, utility return types).
  • If a value can be missing, handle it with typed fallbacks and guards instead of assertions.
  • If a type mismatch appears, fix the contract between layers instead of forcing the type.
// ❌ Avoid
const policy = value as SerializedEditorState<SerializedLexicalNode>;

// ✅ Prefer
const EMPTY_POLICY: SerializedEditorState<SerializedLexicalNode> = {
root: { children: [], direction: 'ltr', format: '', indent: 0, type: 'root', version: 1 }
};
const policy = value || EMPTY_POLICY;