diff --git a/src/Ecs/Data.ts b/src/Ecs/Data.ts index 5eaabcf..2443ba4 100644 --- a/src/Ecs/Data.ts +++ b/src/Ecs/Data.ts @@ -77,45 +77,14 @@ export function Remove(data: DATA, [id, generation]: Id, stat } // Ergonomic Lookup typings -export function Lookup< - DATA extends Data, - A extends keyof DATA, -> ( - data: DATA, - id: Id, - a: A, -): [ - DATA[A][number] | null -]; -export function Lookup< - DATA extends Data, - A extends keyof DATA, - B extends keyof DATA, -> ( - data: DATA, - id: Id, - a: A, - b: B, -): [ - DATA[A][number] | null, - DATA[B][number] | null -]; -export function Lookup< - DATA extends Data, - A extends keyof DATA, - B extends keyof DATA, - C extends keyof DATA, -> ( - data: DATA, - id: Id, - a: A, - b: B, - c: C, -): [ - DATA[A][number] | null, - DATA[B][number] | null, - DATA[C][number] -]; +type ItemType = S extends Record ? T : never; +type StoreType = K extends keyof DATA ? ItemType : never; +type StoreTypes = { + [I in keyof K]: StoreType; +}; +type MaybeStoreTypes = { + [I in keyof K]: StoreType | null; +}; /** * Look up components that may or may not exist for an entity @@ -124,7 +93,7 @@ export function Lookup< * @param components names of components to look for * @returns the cooresponding components, with unfound ones replaced by nulls */ -export function Lookup(data: DATA, [id, generation]: Id, ...components: K[]): ({} | null)[] { +export function Lookup(data: DATA, [id, generation]: Id, ...components: K): MaybeStoreTypes { const entity = data.entity[id]; // inactive entities are fine to lookup, but dead ones are not if(entity && entity.generation == generation && entity.alive != Liveness.DEAD) { @@ -135,9 +104,9 @@ export function Lookup(data: DATA, [id, } else { return null; } - }); + }) as MaybeStoreTypes; } else { - return components.map(() => null); + return components.map(() => null) as MaybeStoreTypes; } } diff --git a/src/Ecs/test.ts b/src/Ecs/test.ts index 69e8160..88960a9 100644 --- a/src/Ecs/test.ts +++ b/src/Ecs/test.ts @@ -144,7 +144,7 @@ export class LoopTest { DumbMotion(this.data, interval); const [triangleDebug] = Lookup(this.data, triangleId, "renderBounds"); - (triangleDebug as RenderBounds).color = "#d40"; + if(triangleDebug) triangleDebug.color = "#d40"; FindCollisions(this.data, 500, (className, sourceId, targetId) => { switch(className) {