You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--!strictlocalmodule=require(script.module)
localfunc=nilfunctionstart()
iffuncthenreturnendfunc=module()
endfunctionstop()
ifnotfuncthenreturnendfunc()
end
script.module:
functionfunc2() -- 2nd function which is returnedendfunctionfunc1() -- 1st function which is initially calledreturnfunc2endreturnfunc1
on line 14 of script, when calling func() the following warning occurs:
(((...any) -> ())?) & (buffer
| class
| function
| number
| string
| table
| thread
| true)
Cannot call a value of type (((...any) -> ())?) & (buffer | class | function | number | string | table | thread | true)
apparently it doesn't see it as just a function and fails to call it unless i cast func to () -> () on line 9 of script:
func=module() :: () -> ()
The text was updated successfully, but these errors were encountered:
The system is inferring a broader type for func, which is why it produces that union type in the error message when you try to call it. Your current workaround of casting func to () -> () is a valid way to resolve the issue; telling the type checker explicitly that func is a function.
You can declare func's type at the top of the script to avoid the ambiguity. So, explicitly stating that func is either nil or a function takes any number of args and returns nothing (a.k.a. () -> ()):
--!strictlocalmodule=require(script.module)
localfunc: (() -> ())? =nil-- declare func's type explicitlyfunctionstart()
iffuncthenreturnendfunc=module()
endfunctionstop()
ifnotfuncthenreturnendfunc() -- no need to cast here anymoreend
The underlying problem here is that type normalization does not seem to handle converting ((...any) -> ()) | nil intersected with ~(false | nil) (the negation expands to that big union you're seeing, which is sort of an orthogonal DX concern) into the particular function type.
The system is inferring a broader type for func, which is why it produces that union type in the error message when you try to call it. Your current workaround of casting func to () -> () is a valid way to resolve the issue; telling the type checker explicitly that func is a function.
You can declare func's type at the top of the script to avoid the ambiguity. So, explicitly stating that func is either nil or a function takes any number of args and returns nothing (a.k.a. () -> ()):
--!strictlocalmodule=require(script.module)
localfunc: (() -> ())? =nil-- declare func's type explicitlyfunctionstart()
iffuncthenreturnendfunc=module()
endfunctionstop()
ifnotfuncthenreturnendfunc() -- no need to cast here anymoreend
doing this was actually the first idea i had but oddly the same warning occurs, i should've mentioned that before
this is the simplest repro i could do of this
script:
script.module:
on line 14 of script, when calling
func()
the following warning occurs:apparently it doesn't see it as just a function and fails to call it unless i cast
func
to() -> ()
on line 9 of script:The text was updated successfully, but these errors were encountered: