# TypeScript Fixes - Prioritized Plan

## Executive Summary

**Current Status**: 566 TypeScript errors remaining  
**Goal**: Reduce to 0 errors  
**Strategy**: Fix by priority, starting with high-impact, easy wins, then systematic pattern fixes

---

## Priority Levels

### 🔴 **Priority 1: Quick Wins (High Impact, Low Effort)**
- **Estimated Errors**: ~50-70
- **Time**: 1-2 hours
- **Impact**: Immediate error reduction, unblocks other fixes

### 🟠 **Priority 2: Pattern-Based Fixes (Medium Impact, Medium Effort)**
- **Estimated Errors**: ~200-250
- **Time**: 3-5 hours
- **Impact**: Significant error reduction through batch fixes

### 🟡 **Priority 3: Type System Fixes (High Impact, High Effort)**
- **Estimated Errors**: ~150-200
- **Time**: 4-6 hours
- **Impact**: Resolves complex type issues

### 🟢 **Priority 4: Schema & Property Fixes (Medium Impact, High Effort)**
- **Estimated Errors**: ~100-150
- **Time**: 3-4 hours
- **Impact**: Aligns code with Prisma schema

---

## Priority 1: Quick Wins 🔴

### 1.1 Missing Imports (~10-15 errors)
**Files Affected**: ~5-8 files  
**Fix Pattern**: Add missing imports
- `uuidv4` from 'uuid'
- `Decimal` from '@prisma/client/runtime/library'
- `Prisma` from '@prisma/client'

**Example Files**:
- `src/core/economics/mrecp/mrecp-harmonization.service.ts` - Missing uuidv4
- `src/core/fx/multiverse-stability/multiverse-fx.service.ts` - Missing Decimal
- `src/core/fx/multiverse-stability/multiverse-ssu.service.ts` - Missing Decimal

**Action**: Batch script to add missing imports

### 1.2 Missing Return Statements in Routes (~30-40 errors)
**Files Affected**: ~15-20 route files  
**Fix Pattern**: Add `return` before `next(error)` or `res.json()`

**Example Files**:
- `src/core/admin/dbis-admin/dbis-admin.routes.ts` - 2 errors
- `src/core/admin/scb-admin/scb-admin.routes.ts` - 8 errors
- `src/core/behavioral/beie/beie.routes.ts` - 2 errors
- `src/core/compliance/gase/gase.routes.ts` - 2 errors
- `src/core/contracts/rssck/rssck.routes.ts` - 1 error

**Action**: Automated script to fix all route return statements

### 1.3 Simple Type Assertions (~10-15 errors)
**Files Affected**: ~5-8 files  
**Fix Pattern**: Add `as unknown as TargetType` for type conversions

**Example**:
```typescript
// Before
metadata: request as Record<string, unknown>

// After
metadata: request as unknown as Record<string, unknown>
```

**Action**: Fix type conversion warnings

---

## Priority 2: Pattern-Based Fixes 🟠

### 2.1 JsonValue Type Mismatches (~100-120 errors)
**Files Affected**: ~40-50 files  
**Fix Pattern**: Cast to `Prisma.InputJsonValue`

**Common Patterns**:
1. `Record<string, unknown>` → `as Prisma.InputJsonValue`
2. `object | null` → `value ? (value as Prisma.InputJsonValue) : Prisma.JsonNull`
3. `JsonValue` → `as Prisma.InputJsonValue`

**High-Impact Files**:
- `src/core/economics/uhem/uhem-encoding.service.ts` - 5 errors
- `src/core/defi/sovereign/defi-module.service.ts` - 1 error
- `src/core/derivatives/gdsl/gdsl-clearing.service.ts` - 1 error
- `src/core/derivatives/gsds/gsds-contract.service.ts` - 2 errors
- `src/core/governance/msgf/msgf-*.service.ts` - Multiple files

**Action**: 
1. Create script to find all JsonValue assignments
2. Batch fix with proper casting
3. Test each file after fixes

### 2.2 Property Access on Unknown Types (~80-100 errors)
**Files Affected**: ~30-40 files  
**Fix Pattern**: Type assertions for `unknown` types

**Common Patterns**:
1. `consolidatedData.bankDetails` where `consolidatedData` is `unknown`
2. `data.property` where `data` is `JsonValue` or `unknown`

**High-Impact Files**:
- `src/core/accounting/reporting-engine.service.ts` - 10 errors
- `src/core/compliance/regtech/sandbox.service.ts` - 8 errors
- `src/core/compliance/regtech/supervision-engine.service.ts` - 1 error

**Action**:
1. Identify all `unknown` type accesses
2. Add proper type guards or assertions
3. Use `as Record<string, unknown>` or specific interfaces

### 2.3 Type Conversion via Unknown (~50-70 errors)
**Files Affected**: ~20-30 files  
**Fix Pattern**: Convert via `unknown` first

**Example**:
```typescript
// Before
metadata: request as Record<string, unknown>

// After
metadata: request as unknown as Record<string, unknown>
```

**High-Impact Files**:
- `src/core/admin/dbis-admin/controls/corridor-controls.service.ts` - 3 errors
- `src/core/admin/dbis-admin/controls/gru-controls.service.ts` - 4 errors
- `src/core/admin/dbis-admin/controls/network-controls.service.ts` - 3 errors
- `src/core/compliance/dscn/dscn-aml-scanner.service.ts` - 1 error
- `src/core/contracts/rssck/rssck.service.ts` - 1 error

**Action**: Batch script to add `as unknown as` for type conversions

---

## Priority 3: Type System Fixes 🟡

### 3.1 Missing Properties on Prisma Types (~60-80 errors)
**Files Affected**: ~25-35 files  
**Fix Pattern**: Use correct Prisma schema fields or add type assertions

**Common Issues**:
1. Accessing `price` on index history (should use `indexValue`)
2. Accessing `bondName`, `bondCode` on bond queries (need to include relations)
3. Accessing `circuitBreakerEnabled` (check schema field name)
4. Accessing `createdAt` in where clauses (may not be filterable)

**High-Impact Files**:
- `src/core/admin/dbis-admin/dashboards/global-overview.service.ts` - 3 errors
- `src/core/admin/dbis-admin/dashboards/gru-command.service.ts` - 15 errors
- `src/core/admin/dbis-admin/dashboards/cbdc-fx.service.ts` - 1 error
- `src/core/compliance/ai/supervisory-ai.service.ts` - 2 errors

**Action**:
1. Review Prisma schema for correct field names
2. Add proper `include` statements for relations
3. Use type assertions where schema doesn't match expectations

### 3.2 Prisma UpdateMany Property Errors (~10-15 errors)
**Files Affected**: ~3-5 files  
**Fix Pattern**: Use correct Prisma update input types

**Issues**:
- `circuitBreakerEnabled` doesn't exist in `GruIndexUpdateManyMutationInput`
- `issuanceWindowOpen` doesn't exist in `GruBondUpdateManyMutationInput`

**Files**:
- `src/core/admin/dbis-admin/controls/gru-controls.service.ts` - 2 errors

**Action**:
1. Check Prisma schema for correct field names
2. Use `update` instead of `updateMany` if needed
3. Or add fields to schema if they're missing

### 3.3 Request Type Extensions (~20-30 errors)
**Files Affected**: ~10-15 files  
**Fix Pattern**: Extend Express Request type or use type assertions

**Issue**: `req.sovereignBankId` doesn't exist on Express Request type

**Files**:
- `src/core/admin/dbis-admin/dbis-admin.routes.ts` - 11 errors
- `src/core/admin/scb-admin/scb-admin.routes.ts` - 8 errors

**Action**:
1. Create type declaration file for Express Request extension
2. Or use `(req as any).sovereignBankId` consistently
3. Or create proper middleware type

### 3.4 Null Safety Checks (~15-20 errors)
**Files Affected**: ~10-15 files  
**Fix Pattern**: Add null checks or use optional chaining

**Issues**:
- `latestPricing.yield` is possibly null
- `index` is possibly null
- Object is possibly null

**Files**:
- `src/core/admin/dbis-admin/dashboards/gru-command.service.ts` - 1 error
- `src/core/fx/multiverse-stability/multiverse-fx.service.ts` - 2 errors
- `src/core/economics/uhem/uhem-analytics.service.ts` - 2 errors

**Action**: Add null checks or use `!` assertion where appropriate

---

## Priority 4: Schema & Property Fixes 🟢

### 4.1 Prisma Schema Mismatches (~30-40 errors)
**Files Affected**: ~15-20 files  
**Fix Pattern**: Align code with Prisma schema or update schema

**Issues**:
- `prisma.settlement` doesn't exist (should be `prisma.gasSettlement`)
- `prisma.commodityToken` doesn't exist
- `prisma.aiAutonomousAction` doesn't exist (should be `prisma.aIAutonomousAction`)
- Missing fields like `divergenceAmount`, `realityId`

**Files**:
- `src/core/compliance/grhs/legal-harmonization.service.ts` - 1 error
- `src/core/compliance/grhs/trade-harmonization.service.ts` - 1 error
- `src/core/governance/scdc/scdc-ai-mandate.service.ts` - 8 errors
- `src/core/economics/mrecp/mrecp-convergence.service.ts` - 4 errors

**Action**:
1. Review Prisma schema
2. Fix code to match schema
3. Or update schema if fields are missing

### 4.2 Complex Type Assignments (~40-50 errors)
**Files Affected**: ~20-25 files  
**Fix Pattern**: Proper type casting and interface matching

**Issues**:
- Array type mismatches (missing `id` field)
- Object spread type errors
- Function parameter type mismatches

**Files**:
- `src/core/admin/dbis-admin/dashboards/global-overview.service.ts` - 1 error
- `src/core/derivatives/gdsl/gdsl-contract.service.ts` - 1 error
- `src/core/derivatives/gsds/gsds-contract.service.ts` - 3 errors
- `src/core/fx/multiverse-stability/multiverse-fx.service.ts` - 1 error

**Action**: Fix type definitions and casts

### 4.3 Decimal Type Operations (~5-10 errors)
**Files Affected**: ~3-5 files  
**Fix Pattern**: Use Decimal methods correctly

**Issues**:
- `number + Decimal` operations
- `Decimal` to `string` conversions

**Files**:
- `src/core/compliance/grhs/regulatory-equivalence.service.ts` - 1 error
- `src/core/cbdc/zk-validation/zk-balance-proof.service.ts` - 1 error

**Action**: Use Decimal methods (`.plus()`, `.toString()`) instead of operators

---

## Implementation Strategy

### Phase 1: Quick Wins (Day 1)
1. ✅ Fix missing imports (1 hour)
2. ✅ Fix missing returns in routes (1 hour)
3. ✅ Fix simple type assertions (30 min)

**Expected Reduction**: 50-70 errors → **~500 errors remaining**

### Phase 2: Pattern Fixes (Day 2-3)
1. ✅ Batch fix JsonValue type mismatches (2-3 hours)
2. ✅ Fix property access on unknown types (2 hours)
3. ✅ Fix type conversions via unknown (1 hour)

**Expected Reduction**: 200-250 errors → **~250-300 errors remaining**

### Phase 3: Type System (Day 4-5)
1. ✅ Fix Prisma property access issues (2-3 hours)
2. ✅ Fix Prisma updateMany errors (1 hour)
3. ✅ Fix Request type extensions (1 hour)
4. ✅ Fix null safety checks (1 hour)

**Expected Reduction**: 150-200 errors → **~50-150 errors remaining**

### Phase 4: Schema & Complex (Day 6-7)
1. ✅ Fix Prisma schema mismatches (2 hours)
2. ✅ Fix complex type assignments (2 hours)
3. ✅ Fix Decimal operations (30 min)

**Expected Reduction**: 100-150 errors → **0 errors remaining**

---

## Tools & Scripts Needed

### 1. Import Fixer Script
```bash
# Find files missing imports and add them
find . -name "*.ts" -exec grep -l "uuidv4\|Decimal" {} \; | \
  xargs grep -L "import.*uuid\|import.*Decimal"
```

### 2. Return Statement Fixer
```bash
# Find catch blocks without return
grep -rn "catch.*{" *.routes.ts | grep -v "return next"
```

### 3. JsonValue Type Fixer
```python
# Script to add Prisma.InputJsonValue casts
# Pattern: Record<string, unknown> → as Prisma.InputJsonValue
```

### 4. Type Conversion Fixer
```python
# Script to add "as unknown as" for type conversions
# Pattern: as TargetType → as unknown as TargetType
```

---

## Success Metrics

- **Phase 1 Complete**: < 500 errors
- **Phase 2 Complete**: < 300 errors
- **Phase 3 Complete**: < 150 errors
- **Phase 4 Complete**: 0 errors

---

## Risk Assessment

### Low Risk (Safe to automate):
- Missing imports
- Missing returns
- Simple type assertions
- JsonValue casts

### Medium Risk (Review needed):
- Property access fixes
- Type conversions
- Null safety checks

### High Risk (Manual review required):
- Prisma schema changes
- Complex type assignments
- Request type extensions

---

## Notes

1. **Test after each phase**: Run build to verify error reduction
2. **Commit frequently**: Small, focused commits per fix category
3. **Document changes**: Note any schema changes or type definition updates
4. **Review Prisma schema**: Some fixes may require schema updates

---

**Last Updated**: Current error count: 566  
**Next Review**: After Phase 1 completion

