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:
2026-05-31 16:24:59 +08:00
parent 9953c7c556
commit 05674a349f
3 changed files with 19 additions and 11 deletions
+8 -6
View File
@@ -6,6 +6,7 @@ import {
type WorldSnapshot,
query,
type RelationshipUpdate,
type WorldEvent,
} from "../src/index";
// ── Definitions ─────────────────────────────────────
@@ -269,8 +270,9 @@ describe("Serialization — complex state", () => {
const playerId = snap.relationships.ownedBy[bulletId];
// Player should have a name
expect(snap.entities[playerId]).toHaveProperty("name");
expect((snap.entities[playerId].name as any).value).toBe("Hero");
const playerComps = snap.entities[playerId];
expect(playerComps).toHaveProperty("name");
expect((playerComps.name as { value: string }).value).toBe("Hero");
});
});
@@ -403,8 +405,8 @@ describe("Serialization — observables after load", () => {
w.spawn();
const loaded = roundTrip(w);
const events: any[] = [];
loaded.events$.subscribe((e: any) => events.push(e));
const events: WorldEvent[] = [];
loaded.events$.subscribe((e) => events.push(e));
const e = loaded.spawn();
expect(events).toHaveLength(1);
@@ -417,8 +419,8 @@ describe("Serialization — observables after load", () => {
w.add(e, Position, { x: 1, y: 2 });
const loaded = roundTrip(w);
const updates: any[] = [];
loaded.observe(query(Position)).subscribe((u: any) => {
const updates: QueryUpdate[] = [];
loaded.observe(query(Position)).subscribe((u) => {
if (u.added.length || u.removed.length || u.changed.length) {
updates.push(u);
}
+3 -2
View File
@@ -5,6 +5,7 @@ import {
query,
type QueryUpdate,
type WorldEvent,
type Entity,
} from "../src/index";
// ── Components ──────────────────────────────────────
@@ -84,11 +85,11 @@ describe("Entity lifecycle", () => {
// ── Components ──────────────────────────────────────
describe("Components", () => {
let world: World;
let entity = 0 as any;
let entity: Entity;
beforeEach(() => {
world = new World();
entity = world.spawn();
entity = world.spawn() as Entity;
});
it("add returns defaults", () => {