What do you want to achieve?
I want to write a type definition for a function that accepts any number of arguments.
What is the issue?
I attempted to put elipses in the type for arguments as you do in functions which is what I would expect to work but instead I get a syntax error.
What solutions have you tried so far?
I’ve tried searching for information on how to do this but I couldn’t find anything about this, not on the DevForum either.
How would I go about doing this? I’m confused as elipses is usually how we define functions with infinite arguments.
The type definition of a function is meant to define what it returns, not the type of function it is.
--!strict
-- no error; function returns a number
local function addNumbers (...) : number
local x,y,z = ...
local result = x + y + z
return result
end
local total = addNumbers(3,4,5)
print(total)
-- error: Type 'string' could not be converted into number
local function combineStrings (...) : number
local a,b,c = ...
local result = tostring(a..b..c)
return result
end
local sentence = combineStrings("Shirovian"," is ", "cool")
print(sentence)
I believe using a variadic type like ...any or ...number is what you want. What it does is accept any number of arguments of the type specified.
--!strict
type genericEllipseFunction = (...any) -> ({})
local myFunction : genericEllipseFunction = function(...)
local tableOfArgs = {...}
return tableOfArgs
end
local myTable = myFunction(3,"Apples are orange", true, {[3] = false, [6] = true})
print(myTable)
type numberEllipseFunction = (...number) -> number
local addNumbers : numberEllipseFunction = function (...)
local x,y,z = ...
return x + y + z
end
local result = addNumbers(3,5,2)
print(result)