refactor: improve type safety in World and tests
Replace `any` types with specific interfaces like `WorldEvent`, `QueryUpdate`, and `Entity` to strengthen type checking. This includes refining the deserialization logic in `World.fromSnapshot` to use properly typed component definitions.
This commit is contained in:
+8
-3
@@ -7,6 +7,7 @@ import { ObservableLayer } from "./observable/observe";
|
||||
import type { QueryUpdate, RelationshipUpdate } from "./observable/events";
|
||||
import type { RelationshipDef } from "./relationship";
|
||||
import { Observable } from "rxjs";
|
||||
import type { WorldEvent } from "./observable/events";
|
||||
import type { WorldSnapshot } from "./serialization";
|
||||
|
||||
// ── World ─────────────────────────────────────────────
|
||||
@@ -40,7 +41,7 @@ export class World {
|
||||
private _observable = new ObservableLayer();
|
||||
|
||||
/** Global event stream. */
|
||||
get events$(): Observable<any> {
|
||||
get events$(): Observable<WorldEvent> {
|
||||
return this._observable.events$.asObservable();
|
||||
}
|
||||
|
||||
@@ -435,7 +436,10 @@ export class World {
|
||||
): World {
|
||||
const world = new World();
|
||||
|
||||
const compByName = new Map(components.map((c) => [c.name, c]));
|
||||
const compByName = new Map(components.map((c) => [c.name, c])) as Map<
|
||||
string,
|
||||
ComponentDef<Record<string, unknown>>
|
||||
>;
|
||||
const relByName = new Map((relationships ?? []).map((r) => [r.name, r]));
|
||||
|
||||
// Map string ids → real Entity handles
|
||||
@@ -453,7 +457,8 @@ export class World {
|
||||
`Pass it in the components array.`,
|
||||
);
|
||||
}
|
||||
world.add(entity, def, value as any);
|
||||
// Unknown at deserialization boundary; shape matches ComponentDef.defaults
|
||||
world.add(entity, def, value as Partial<Record<string, unknown>>);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user