Specifying parameter type as a function

I’ve been trying to figure out how I’d make the parameter data type for typechecking a function. Example:

--!strict

local function ExecuteAfter5Seconds(FunctionToExecute : function)
   task.wait(5)
   FunctionToExecute()
end

local function Hello()
   print("Hello world!")
end

ExecuteAfter5Seconds(Hello)

But this just results in it erroring with “Expected type, got ‘function’” in

local function ExecuteAfter5Seconds(FunctionToExecute : function)

Is there any way I can achieve this without it erroring?

1 Like

Use (type...) -> type syntax to declare a function param

local function RunCallback(name: strict, callback: (string) -> ())
  callback(name)
end

() is a special case which means return nothing. Here’s another example which uses a return and two params

local function ManipulateNumberWithCallback(a: number, b: number, manipulator: (number, number) -> number)
  return manipulator(a, b)
end
14 Likes

Use type() or typeof() to ascertain the type of a value, in Lua functions are recognised as first class values.