luau icon indicating copy to clipboard operation
luau copied to clipboard

'Cannot call non-function' error despite both union types being functions

Open BizzarBlitz opened this issue 2 years ago • 2 comments

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")

BizzarBlitz avatar Jan 29 '23 22:01 BizzarBlitz

I think you need to use intersection syntax instead func1 & func2.

Although maybe the error message could better indicate that?

JohnnyMorganz avatar Jan 30 '23 12:01 JohnnyMorganz

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.

andyfriesen avatar Apr 06 '23 17:04 andyfriesen