Skip to content

QueryResult:snapshot has untyped iter metamethod #12

Description

@fractalbeauty

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions