luau
luau copied to clipboard
Iterating a table with pairs incorrectly appends type
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)

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)