Tables with an intersection type show the warning “Cannot call a value of type a & b” when iterating through them, even if the type includes a __iter
function
Example:
--types
type a = {
__iter: (self: a) -> (
(t: a, lastKey: number?) -> (number?, string?),
a
)
}
type b = {}
type ab = a & b
--metatable with __iter
local mtbl = {
__iter = function(self: a)
return function(t: a, lastKey: number?)
local i = lastKey and lastKey + 1 or 1
if t[i] then
return i, t[i] .. "v"
else
return nil
end
end, self
end,
}
--tables
local a: a = {1, 2, 3, 4, 5} :: a
setmetatable(a, mtbl)
local ab: ab = {1, 2, 3, 4, 5} :: ab
setmetatable(ab, mtbl)
--iterate
for k, v in a do
print(k, v)
end
for k, v in ab do
print(k, v)
end
The code works as expected. However, the ab
on the third line from the bottom for k, v in ab do
is highlighted with the following warning: