Lua question (type declaration)

Hello guys !

I just found out this type declaration in the matter repository :

type System = (...any) -> () | { system: (...any) -> (), event: string?, priority: number?, after: nil | {} }

I can’t find any information about it on internet. Do you know what part of lua doc should I refer to?

Thanks !

The most reliable resources are Type checking - Luau and Type Checking | Documentation - Roblox Creator Hub. For any particular questions I usually turn to the DevForum and look around if they were already answered anywhere.

What is type System?

1. Unions

| → constructs unions.

local strOrNum: string | number = "something" -- ok
local strOrNum: string | number = 1 -- ok
local strOrNum: string | number = {} -- not ok

2. A function or a table

System is either a function or a table with particular properties.

2.1. As a function:

() -> () → a function
→ The first parentesis indicates types of arguments the function accepts.
→ The parentesis after -> indicates types the function returns.

Parentesis may sometimes be ommited, e.g. number -> string, but are neccessary when the function takes no arguments, returns no arguments, or accepts and/or takes multiple arguments.

(...any) -> () takes variadic arguments of any type; returns void/nothing.

General function examples
local function FnIncorrect(n: number): string end -- not ok (must return string)
local function FnCorrect(n: number): string -- ok
	return tostring(n)
end

The above function can have types declared differently.

-- must return string, expects 'n' to be a number
local FnIncorrect: Function = function(n) -- not ok
	return n
end
FnIncorrect(5) -- ok
FnIncorrect("L") -- not ok

-- must return string, expects 'n' to be a number
local FnCorrect: Function = function(n) -- not ok
	return tostring(n)
end
FnCorrect(5) -- ok
FnCorrect("L") -- not ok
type Function = (...number) -> ()
local Fn: Function = function(...) end
local FnIncorrect: Function = function(...) return "L" end -- not ok
Fn(1, "2") -- not ok (numbers expected)
Fn(3, 4) -- ok

Essentially, the function can take any number of arguments of any type, returning nothing.

2.2. As a table:

Should System be a table, it must have the listed keys of appropriate type.

Side note: ? allows either the annotated type or nil.

  • System.system must be a function accepting any number of arguments of any type and return nothing/void.
  • System.event may either be string or nil.
  • System.priority may be either number or nil.
  • System.after may be either nil or a table with arbitrary values.

Wow I wasn’t expecting a so well construted answer !
Thanks a lot !