AgentSkillsCN

fix-type-errors

针对 Python(mypy/pyright)与 TypeScript(vue-tsc)的系统性类型错误解决。适用于修复类型错误、解决类型检查失败,或优化类型导入时使用。

SKILL.md
--- frontmatter
name: fix-type-errors
description: Systematic type error resolution for Python (mypy/pyright) and TypeScript (vue-tsc). Use when fixing type errors, resolving type check failures, or optimizing type imports.

Type Error Resolution

Systematic workflow to resolve static type checker errors while preserving runtime behavior.

Scope

Applies when:

  • Fixing mypy, pyright, or vue-tsc errors
  • Resolving type check failures in CI
  • Optimizing imports for type-only dependencies
  • Eliminating # type: ignore suppressions

Core Principles

PrincipleDescription
Behavior PreservationFixes must NOT change runtime logic. Only annotations, imports, and casts.
Zero Suppressions# type: ignore / // @ts-ignore are forbidden. Resolve properly or escalate.
Import OptimizationUse TYPE_CHECKING (Python) or import type (TypeScript) for type-only imports.
Minimal SurfaceOne error → one minimal fix. Avoid broad refactors.

Commands

Backend (Python)

PriorityCommandUse Case
1. Makefilemake -C backend type-checkFull check (black, isort, flake8, mypy, pyright)
2. Directcd backend && poetry run pyright src/ tests/Pyright only
3. Single-filecd backend && poetry run pyright path/to/file.pyFast iteration

Frontend (TypeScript/Vue)

PriorityCommandUse Case
1. Makefilemake -C frontend lint type-checkFull check (ESLint + vue-tsc)
2. Directcd frontend && npm run type-checkvue-tsc only
3. Single-filecd frontend && npx vue-tsc --noEmit src/path/to/file.tsFast iteration

Workflow

Phase 1: Error Discovery

  1. Run type checker and capture output
  2. Parse into structured list: [file:line] error_code: message
  3. Categorize:
CategoryExamplesAction
A: Direct FixMissing annotation, wrong typeFix immediately
B: Import OptimizationCircular import, runtime-only importMove to TYPE_CHECKING
C: StructuralGeneric variance, protocol mismatchAnalyze deeply
D: ExternalUntyped library, upstream bugEscalate

Phase 2: Resolution

For each error, apply decision tree:

  1. Generated code? (*_generated/) → Skip
  2. Fixable with annotation? → Add type hint
  3. Type narrowing issue? → Add isinstance/assert/guard
  4. Import-only-for-types? → Move to TYPE_CHECKING block
  5. Library typing issue? → Add cast() with documentation
  6. None apply? → Follow Suppression Protocol

Phase 3: Validation

After fixes, run validation:

Change TypeCommands
Pure annotationsmake format type-check (no tests)
Added cast/assertmake format type-check test
Changed runtime importsmake format type-check test

Common Patterns

Python: TYPE_CHECKING Import

python
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from expensive_module import ExpensiveType

def func(param: "ExpensiveType") -> None:
    ...

Python: Cast for Library Gaps

python
from typing import cast
result = cast(ExpectedType, untyped_call())  # Document why safe

TypeScript: Type-Only Import

typescript
import type { SomeType } from './module'  // Erased at runtime
import { SomeValue } from './module'       // Kept at runtime

Suppression Protocol

Only after exhausting all fix approaches:

  1. Document root cause — Why proper fix is impossible
  2. Propose specific suppression# pyright: ignore[reportAssignmentType]
  3. Validate — Run type checker to confirm resolution
  4. Report to user — Format:
code
⚠️ SUPPRESSION REQUIRED (External Issue)

File: path/to/file.py:42
Error: [reportAssignmentType] Type "X" is not assignable to "Y"

Root Cause: [Library/framework limitation]

Proposed: __tablename__ = "users"  # pyright: ignore[reportAssignmentType]

Validation: ✅ Tested — resolves error

DO NOT apply suppressions without user acknowledgment.

Anti-Patterns

  • ❌ Using # type: ignore without exhausting fixes
  • ❌ Modifying runtime behavior to satisfy type checker
  • ❌ Editing generated code (*_generated/ directories)
  • ❌ Broad refactors when targeted fix suffices