dustinlacewell / react-ecs

An entity-component-system for React
https://react-ecs.ldlework.com/
MIT License
121 stars 18 forks source link

Incorrect definite assertion for Query #3

Open Honga1 opened 3 years ago

Honga1 commented 3 years ago

fluff

Hi :D. I'm exercising the library pretty well at the moment. Well done on it. So far I've only ran into type issues with things like twice extending the Facet class, which wouldn't then carry props into the react component.

Line causing type error

https://github.com/dustinlacewell/react-ecs/blob/d06850bea1847f87b9b8edefa62693790ded7843/libs/core/src/lib/Query.ts#L20

How I found this

Regarding the line above: If I have a query

// Note 'hasAny'
const query = useQuery((entity) => entity.hasAny(FacetA, FacetB))

Then follow up with a loop:

query.loop([FacetA, FacetB], (entity, [facetA, facetB]) => {
  assert(facetA !== undefined)
  assert(facetB !== undefined)
}

Would likely throw if I only added one of the two facets FacetA FacetB. Even though they are definitely asserted on your get(t)! part.

Indeed I can reproduce this here: https://codesandbox.io/s/zealous-nobel-q5id3?file=/src/App.tsx

Suggested fixes

One of: