Types Organization in TypeScript Projects: Mercury Bot Case Study
Types are the backbone of any TypeScript project, but as the codebase grows, they can become scattered and hard to maintain. Here's a practical guide on organizing types, with a real-world example from Mercury Bot.
General Approaches to Type Organization
1. Domain-Driven Type Organization
Keep types close to their domain logic, organized by feature. This approach makes it easy to find related types and maintains a clear connection between types and their usage.
src/
├── market-ranking/
│ ├── types/
│ │ ├── state.types.ts
│ │ ├── config.types.ts
│ │ └── index.ts
2. Shared-First with Domain Specifics
Separate shared types from domain-specific ones, reducing duplication while maintaining clarity.
src/
├── types/
│ ├── common/
│ │ └── models.types.ts
│ └── domain/
│ └── market-ranking.types.ts
3. Layer-Based Type Organization
Organize types by technical layers, making it clear where each type belongs in the architecture.
src/
├── types/
│ ├── api.types.ts
│ ├── models.types.ts
│ └── services.types.ts
4. Interface-First with Implementation Types
Separate interface definitions from implementation types, ideal for dependency inversion.
src/
├── interfaces/
│ └── market-ranking.interface.ts
└── features/
└── market-ranking/
└── types/
└── implementation.types.ts
5. Capability-Based Organization
Align types with system capabilities, perfect for microservices and modular architectures.
src/
├── capabilities/
│ └── market-ranking/
│ ├── types.ts
│ └── index.ts
Mercury Bot: Current State and Improvement Plan
Current State
Currently, Mercury Bot has most of its types concentrated in:
src/market-ranking/types.ts(400+ lines)src/ta/types.ts- Various smaller type definitions scattered across components
Proposed Reorganization
Here's a focused plan to split and reorganize types without overwhelming changes:
-
Market Ranking Types Split
- Location:
src/market-ranking/types/ - New Files:
state.types.ts- GistState and related state interfacesmodels.types.ts- Model enums and weights interfacescomparison.types.ts- Comparison related interfacesconfig.types.ts- Configuration interfacesindex.ts- Re-exports all types
- Location:
-
Technical Analysis Types
- Location:
src/ta/types/ - New Files:
indicators.types.ts- Indicator enums and interfacestimeframes.types.ts- TimeFrame related typesindex.ts- Re-exports
- Location:
-
Shared Types
- Location:
src/common/types/ - New File:
queue.types.ts- Queue related types and constants
- Location:
Migration Strategy
- Create new type files with proposed structure
- Move types to their new locations
- Update imports across the codebase
- Remove old type files
- Update documentation
Benefits
- Smaller, more focused files
- Clear organization by domain
- Easier to find related types
- Maintainable file sizes
- Clear import paths
Type Discovery Rules
- Always start with domain directory's
types/folder - Check
common/types/for shared types - Use IDE's "Go to Definition" feature
- Follow re-exports from
index.tsfiles
Conclusion
Type organization doesn't have to be overwhelming. Start with a clear plan, focus on the most impactful changes, and maintain consistency. The proposed changes for Mercury Bot demonstrate how to apply these principles in practice while keeping the scope manageable.
Remember: The goal is to make the codebase more maintainable and types more discoverable, not to achieve theoretical perfection.
