Skip to content

Migrate Backend to Strict TypeScript #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
284 changes: 284 additions & 0 deletions TYPESCRIPT_PROJECT_REFERENCES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
# TypeScript Project References Configuration

This document describes the TypeScript project references setup for improved compilation performance and better project organization in large codebases.

## Overview

The project has been configured with TypeScript project references to enable:
- **Incremental compilation**: Only rebuild changed projects
- **Composite builds**: Better dependency management between modules
- **Parallel compilation**: Build independent projects concurrently
- **Type checking performance**: Faster type checking through project boundaries
- **Build caching**: Persistent build information for faster subsequent builds

## Project Structure

The TypeScript configuration is organized into the following projects:

```
├── tsconfig.json # Root configuration with project references
├── tsconfig.base.json # Shared compiler options
├── tsconfig.core.json # Core types and utilities
├── tsconfig.api.json # API contracts and domain models
├── tsconfig.rpc.json # RPC system components
├── tsconfig.services.json # Service layer and repositories
├── tsconfig.validation.json # Validation and middleware components
├── tsconfig.integration.json # Integration and boundary components
└── tsconfig.grammar.json # Grammar definition
```

## Project Dependencies

The projects are organized with clear dependency relationships:

```
tsconfig.core.json (no dependencies)
├── tsconfig.api.json
│ ├── tsconfig.rpc.json
│ ├── tsconfig.services.json
│ └── tsconfig.validation.json
├── tsconfig.integration.json (depends on all above)
└── tsconfig.grammar.json (no dependencies)
```

### Core Project (`tsconfig.core.json`)
**Purpose**: Foundational types and utilities
**Dependencies**: None
**Contents**:
- `branded-types.ts` - Type-safe ID and domain string types
- `rpc-types.ts` - Core RPC type definitions
- `type-transformers.ts` - Type transformation utilities

### API Project (`tsconfig.api.json`)
**Purpose**: API contracts and domain models
**Dependencies**: Core
**Contents**:
- `api-contracts.ts` - API interface definitions
- `domain-models.ts` - Internal domain model types
- `type-transformers.ts` - Data transformation utilities

### RPC Project (`tsconfig.rpc.json`)
**Purpose**: RPC system implementation
**Dependencies**: Core, API
**Contents**:
- `rpc-methods.ts` - RPC method implementations
- `rpc-router.ts` - Request routing logic
- `rpc-errors.ts` - Error handling
- `enhanced-rpc-methods.ts` - Enhanced RPC implementations
- RPC validation and examples

### Services Project (`tsconfig.services.json`)
**Purpose**: Service layer and repository patterns
**Dependencies**: Core, API
**Contents**:
- `service-interfaces.ts` - Service layer contracts
- `repository-interfaces.ts` - Repository pattern definitions
- `dependency-injection.ts` - DI container types
- `service-factories.ts` - Service factory patterns

### Validation Project (`tsconfig.validation.json`)
**Purpose**: Validation and middleware components
**Dependencies**: Core, API, RPC
**Contents**:
- `api-boundary-validation.ts` - Request/response validation
- `api-transformation-middleware.ts` - Data transformation middleware
- `simple-validation.ts`, `zod-validation.ts` - Validation implementations
- `validation-examples.ts` - Validation usage examples

### Integration Project (`tsconfig.integration.json`)
**Purpose**: Component integration and boundaries
**Dependencies**: Core, API, RPC, Services, Validation
**Contents**:
- `component-boundaries.ts` - Component boundary definitions
- `index.ts` - Main entry point

### Grammar Project (`tsconfig.grammar.json`)
**Purpose**: Tree-sitter grammar definition
**Dependencies**: None
**Contents**:
- `grammar.ts` - Tree-sitter grammar specification

## Build Scripts

The following npm scripts are available for building:

### Primary Build Commands
```bash
# Build all projects with dependencies
npm run compile

# Build with incremental compilation (faster rebuilds)
npm run compile:incremental

# Watch mode for development
npm run compile:watch

# Clean all build artifacts
npm run compile:clean
```

### Individual Project Builds
```bash
npm run compile:core # Build only core types
npm run compile:api # Build API layer
npm run compile:rpc # Build RPC system
npm run compile:services # Build service layer
npm run compile:validation # Build validation layer
npm run compile:integration # Build integration layer
npm run compile:grammar # Build grammar definition
```

### Type Checking and Validation
```bash
npm run type-check # Dry run with verbose output
npm run type-check:noEmit # Type check without emitting files
npm run validate:project-references # Validate project references setup
```

## Build Performance Features

### Incremental Compilation
Each project has its own `.tsbuildinfo` file for tracking compilation state:
- `./dist/core/.tsbuildinfo`
- `./dist/api/.tsbuildinfo`
- `./dist/rpc/.tsbuildinfo`
- `./dist/services/.tsbuildinfo`
- `./dist/validation/.tsbuildinfo`
- `./dist/integration/.tsbuildinfo`
- `./dist/grammar/.tsbuildinfo`

### Composite Mode
All projects are configured with `"composite": true` enabling:
- Declaration file generation with source maps
- Dependency tracking between projects
- Incremental builds based on project boundaries

### Source Maps and Declarations
Each project generates:
- **Declaration files** (`.d.ts`) for type information
- **Declaration maps** (`.d.ts.map`) for IDE navigation
- **Source maps** (`.js.map`) for debugging

## Development Workflow

### Initial Setup
```bash
# Install dependencies
npm install

# Initial full build
npm run compile
```

### Development Mode
```bash
# Start watch mode for automatic rebuilds
npm run compile:watch
```

### Production Build
```bash
# Clean previous builds
npm run compile:clean

# Full production build
npm run compile

# Complete build with native components
npm run build
```

### Selective Rebuilds
When working on specific areas:
```bash
# Working on core types
npm run compile:core

# Working on API contracts
npm run compile:api

# Working on services
npm run compile:services

# Working on validation
npm run compile:validation
```

## Migration Scope Management

The project references help manage migration scope by:

1. **Isolated Changes**: Modify one project without affecting others
2. **Dependency Validation**: TypeScript validates cross-project dependencies
3. **Incremental Migration**: Migrate projects individually
4. **Type Safety**: Maintain type safety across project boundaries

## Performance Benefits

### Compilation Speed
- **Parallel Builds**: Independent projects build concurrently
- **Incremental Updates**: Only changed projects rebuild
- **Dependency Caching**: Unchanged dependencies use cached results

### Development Experience
- **Faster Type Checking**: Isolated type checking per project
- **Better IDE Performance**: Projects load independently
- **Selective Compilation**: Build only what you're working on

## Troubleshooting

### Common Issues

#### Build Errors
```bash
# Clean and rebuild if you encounter stale build issues
npm run compile:clean
npm run compile
```

#### Dependency Issues
```bash
# Check project dependency order
npm run type-check
```

#### Performance Issues
```bash
# Use verbose mode to identify bottlenecks
npm run compile -- --verbose
```

### Debugging Project References
```bash
# Dry run to see what would be built
npm run type-check

# Build with detailed logging
npm run compile -- --verbose --listFiles
```

## Best Practices

### Adding New Files
1. Add files to the appropriate project's `include` array
2. Ensure dependencies are correctly configured in `references`
3. Test with `npm run compile:projectname`

### Modifying Dependencies
1. Update the `references` array in dependent projects
2. Rebuild dependent projects after changes
3. Use `npm run compile:clean` if dependency changes seem cached

### Performance Optimization
1. Keep projects focused and cohesive
2. Minimize cross-project dependencies
3. Use incremental builds during development
4. Clean builds for production deployments

## Future Enhancements

Potential improvements to consider:
- **Workspace support** for multi-package repositories
- **Custom build tools** integration
- **CI/CD optimization** using project references
- **Bundle optimization** based on project boundaries
Loading