You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: improve type safety, reduce code duplication, and optimize error handling (Tasks 02, 03, 04)
## Task 02: Type Safety Improvements
### 2.1 Command Discriminated Union
- Converted `Command` interface to discriminated union type
- Enables TypeScript type narrowing for safer command processing
- Removed runtime checks for `componentType` existence in command processing
### 2.2 SerializedWorld Type
- Defined `SerializedEntityIdManager` interface for proper type safety
- Replaced `any` type in `SerializedWorld.entityManager`
### 2.3 Type Aliases
- Added `AnyComponentId` and `AnyEntityId` type aliases
- Documents intentional type erasure for internal runtime storage
### 2.4 Non-null Assertions
- Removed non-null assertions (!.) in favor of type-safe code
- Added explanatory comments where TypeScript type narrowing guarantees safety
- Applies to `serialization.ts` and `world.ts` relation handling
## Task 03: Code Duplication Elimination
### 3.1 Wildcard Relation Helpers
- Extracted `findWildcardRelations()` and `hasWildcardRelation()` helpers
- Added to `archetype-helpers.ts` for reuse across codebase
- Updated `world.ts` to use new helpers, eliminating ~15 lines of duplication
### 3.2 Utility Function Merge
- Merged identical `getOrComputeCache` and `getOrCreateWithSideEffect` functions
- Created unified `getOrCompute` function with backward-compatible aliases
- Reduced redundant code in `utils.ts`
## Task 04: Error Handling Optimization
### 4.1 Hook Try-Catch Removal
- Replaced try-catch in `entityHasAllComponents` with `getOptional()`
- Replaced try-catch in `entityHadAllComponentsBefore` with `getOptional()`
- Improves performance and code clarity
### 4.2 ID Type Functions
- Replaced try-catch in `getIdType()` with `decodeRelationRaw()`
- Replaced try-catch in `getDetailedIdType()` with `decodeRelationRaw()`
- Replaced try-catch in `inspectEntityId()` with `decodeRelationRaw()`
- Uses null-returning function instead of exception-based control flow
## Testing
- All 263 tests pass
- TypeScript compilation successful with `bunx tsc --noEmit`
- No breaking changes to public API
0 commit comments