String.gsub incorrectly typed for multiple captures

string.gsub’s third parameter allows you to pass a function which should return the final replacement string. The params of this function are dependant on how many captures there are from the input string + the pattern. However, the typing on this function seems to be incorrect as it seems to only ever expect one param regardless of number of captures.

local someString = "Hello world!"

local result = string.gsub(someString, "([%w]+) ([%w]+)", function(a, b)
	-- this is valid, but the lint yells at you
	return b .. " " .. a
end)

print(result) -- world Hello!
2 Likes

Thanks for the report! I filed a ticket in our internal database and we’ll follow up when we have an update for you.

1 Like

Thanks for the report, this issue seems to be fixed in the New Type Solver. The arity of the function being passed in now matches the number of captures in gsub. You may experience bugs with inferring the types for the callback right now, but that’s a separate issue in the New Type Solver that we’re working on actively.

2 Likes