Syntax error when using elipses in function type definition

  1. What do you want to achieve?
    I want to write a type definition for a function that accepts any number of arguments.

  2. 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.
    image

  3. 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)
1 Like

By type definition I mean this:

type Foo = (arg) -> ()

My bad for not clarifying, thank you still for the reply. :smile:

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)

There’s some documentation I found here.

1 Like

Thank you, that was it! Didn’t know variadic types shouldn’t use colons in types, somewhat confusing. :slightly_frowning_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.