luau
luau copied to clipboard
Defining functions in a table with a defined type, doesn't give the functions arguments types
In the following code the types for the functions arguments should already be defined, but are instead infered by the new solver
type VaradicParserPrototype<R> = {
__call: (self: VaradicParser<R>, args: { string }) -> (boolean?, { R }),
__index: VaradicParserPrototype<R>,
varadic: boolean,
}
type VaradicParser<R> = typeof(setmetatable({} :: { parser: (string) -> R }, {} :: VaradicParserPrototype<R>))
local varadic_parser = {} :: VaradicParserPrototype<any>
varadic_parser.__index = varadic_parser
-- infered correctly, setting this to a type thats not a boolean results in a type error
varadic_parser.varadic = true
-- var_parser is infered as { read parser: never } and args is infered as { [unknown]: unknown }
-- even though var_parser should be infered as VaradicParser<any> and args should be { string }
-- with its return infered as (boolean, { nil })
function varadic_parser.__call(var_parser, args)
local parser = var_parser.parser
local values = {}
for index, arg in args do
local success, value = parser(arg)
if success then
table.insert(values, value)
else
return nil, values
end
end
return true, values
end