Is it possible to use Function Overloading in Lua?

Yes. You can typecast your function using & for function overloading.

--!strict

local module = {}

module.foo = function(a: any, b: any): ()
	if typeof(a) ~= typeof(b) then
		error(`invalid argument(s)`, 2)
	end
	
	-- code
end :: ((a: string, b: string) -> ()) & ((a: number, b: number) -> ())

module.foo(1, 2)
module.foo("a", "b")
-- type error
module.foo(1, "b")
-- type error
module.foo("a", 2)

return module
5 Likes