Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryResult:snapshot has untyped iter metamethod #12

Open
hazelmeow opened this issue Jul 25, 2023 · 0 comments
Open

QueryResult:snapshot has untyped iter metamethod #12

hazelmeow opened this issue Jul 25, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@hazelmeow
Copy link

Currently it's typed as returning ReadonlyArray<[Entity<T>, ...T]>, but the return value actually has an __iter metamethod that interferes with things.

The metamethod lets you do this in Lua (the same as iterating a query without snapshotting it):

for entityId, health, player in world:query(Health, Player):snapshot() do

But in TS if we do something like .snapshot().mapFiltered(([id, health, player]) => {}), we'll generate something like:

local callback = function(_param)
    local id = _param[1]
    local cpn = _param[2]
    local player = _param[3]
end
--  ReadonlyArray.mapFiltered 
local _newValue = {}
local _length = 0
for _k, _v in querySnapshot do
    local _result = _arg0(_v, _k - 1, querySnapshot)
    if _result ~= nil then
        _length += 1
        _newValue[_length] = _result
    end
end
--  ReadonlyArray.mapFiltered 

And because of __iter, _k will end up being the id instead of the index and _v will be health, not [id, health, player], and so the callback will not get the value that the types think it will

@LastTalon LastTalon added the bug Something isn't working label Jan 8, 2024
Ukendio pushed a commit that referenced this issue Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants