luau
luau copied to clipboard
'Cannot call non-function' error despite both union types being functions
Luau type analysis says that I cannot call a non-function, despite both types being the exact same function type.
type func1 = (param: any) -> ()
type func2 = (param: any) -> ()
local func: func1 | func2 = function(param: any)
end
func("Parameter")
--^^ Type error: Cannot call non-function ((any) -> (any)) | ((any) -> (any))
To work around this issue, I have to assert either one type of the other, which I shouldn't have to do considering the types are identical.
--Neither of these cause type errors, but I sacrifice some syntax highlighting and readability
(func :: func1)("Parameter");
(func :: func2)("Parameter")
I think you need to use intersection syntax instead func1 & func2.
Although maybe the error message could better indicate that?
Unions of functions are not a priority right now because it's so rare that they are actually useful. You usually want an intersection of functions.