luau icon indicating copy to clipboard operation
luau copied to clipboard

Iterating a table with pairs incorrectly appends type

Open ArchLand64 opened this issue 2 years ago • 0 comments

The code below fails to typecheck the second incorrect index of prices because pairs is appending [string]: a to the type of prices.

--!strict
local prices = {
	hat = 1,
	bat = 2,
}
print(prices.wwwww)
for _, _ in pairs(prices) do
end
print(prices.wwwww)

image

Right now, you can work around this issue by typecasting a table before you pass it to pairs.

local prices = {
	hat = 1,
	bat = 2,
}
print(prices.wwwww)
for _, _ in pairs(prices :: {[string]: number}) do
end
print(prices.wwwww)

ArchLand64 avatar Feb 22 '23 23:02 ArchLand64