Cleanup types

From NVlabs/Skill2Env · View interactive version

Cleanup types is an AI agent skill from the GitHub repository NVlabs/Skill2Env. Find duplicated or fragmented type/interface definitions across files and consolidate to a shared types module. TypeScript-first; also handles Python dataclasses/TypedDicts and Go structs. Use when the user asks to consolidate types, find duplicate interfaces, or organize type definitions. Example q

How to use this skill

Skill file

Find type definitions that should be shared but are duplicated across files. Consolidate into a single source of truth. Conservative by default — type consolidation looks safe but can hide intentional divergence.

Preflight

  1. Language detect: TS/JS (primary), Python (dataclass, TypedDict, Pydantic), Go (struct), Rust (struct/enum).
  2. Git state: refuse auto-apply on dirty tree.
  3. Report dir: ensure exists.
  4. Identify type homes: where does the project already keep shared types? Look for types/, models/, schema/, package boundaries (e.g., monorepos often colocate DB types under packages/db/src/schema/).

Detect

No off-the-shelf tool reliably finds semantically equivalent types across languages. Use AST scanning + grep + structural comparison.

TypeScript

# Find every `interface Foo` and `type Foo =` declaration
# Group by name, then by structural shape
grep -rn --include="*.ts" --include="*.tsx" -E "^export (interface|type) [A-Z]" . > /tmp/ts-types.txt

For each name that appears in 2+ files, compare the shapes. Use tsc --listFiles --noEmit output combined with the TypeScript compiler API if available, OR fall back to textual comparison after normalizing whitespace.

Python

# Find dataclass / TypedDict / Pydantic models
grep -rn --include="*.py" -E "^(class \w+\(.*?(BaseModel|TypedDict)\)|@dataclass)" . > /tmp/py-types.txt

Go

grep -rn --include="*.go" -E "^type \w+ struct" . > /tmp/go-types.txt

Rust

grep -rn --include="*.rs" -E "^pub (struct|enum) \w+" . > /tmp/rust-types.txt

Cross-reference: for each type name, check if multiple files define it. For each pair, compare field-by-field.

Assess

Write .claude/cleanup-reports/cleanup-types-{YYYY-MM-DD}.md:

# Type Consolidation Assessment — YYYY-MM-DD

## Summary
- Type names with 2+ definitions: N
- HIGH (structurally identical): X
- MEDIUM (overlapping fields, may diverge intentionally): Y
- LOW (same name, different meaning): Z

## Findings

### `User` interface — HIGH
- Definitions:
  - `apps/app/features/auth/types.ts:8` — `{ id: string; email: string; createdAt: Date }`
  - `apps/admin/features/users/types.ts:12` — `{ id: string; email: string; createdAt: Date }`
- Identical shape. Consolidate to `packages/db/src/schema/user.ts` (already has the runtime model).

### `Account` interface — MEDIUM
- `domains/account/types.ts` — has 8 fields including `permissions: string[]`
- `features/billing/types.ts` — has 5 of those 8 fields, no `permissions`.
- Likely the billing one is a deliberate slice. Recommend: keep both, but rename billing's to `BillingAccount` for clarity. Don't auto-apply.

### `Config` type — LOW (different concepts)
- `lib/api-config.ts` — API client config
- `lib/feature-config.ts` — feature flag config
- Same name, unrelated. Recommendation: rename one for clarity, but no consolidation.

## Critical Assessment
[2-3 paragraphs: are types fragmented because there's no shared package, or because the architecture intentionally separates concerns?]

Apply

Auto-consolidate HIGH only. Type consolidation often crosses package boundaries and reshapes import graphs — be conservative.

Confidence rubric

HIGH (auto-apply):

MEDIUM (report only):

LOW (note only):

Execution (HIGH only)

  1. Choose canonical home: existing shared types module that both source files can import.
  2. Move the type definition there (preserve JSDoc/comments).
  3. Replace both originals with import type { Foo } from '...'.
  4. Run typecheck — if any error appears (often due to nominal typing differences in TS, or generic param mismatches), revert that one and downgrade.
  5. Commit: chore(cleanup): cleanup-types — consolidated N duplicate type definitions.

Verify

# Typecheck is the critical signal
bun run typecheck 2>&1 || npx tsc --noEmit
mypy . 2>&1 || true
go build ./... 2>&1
cargo check 2>&1

# Then standard test/lint
bun test && bunx biome check .

Type consolidation that breaks downstream usage (e.g., a consumer relied on a field being optional in one definition but required in the other) shows up here. Revert on any new error.

Output

NEVER

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.