Luau type check, how to avoid: Type 'RemoteEvent?' does not have key 'FireServer'

My issue is that I I cannot call a function on a Luau type which has a nil union type added to the type definition.

Specificaly, I cannot call the FireServer function on a type defined as local REGameItems : RemoteEvent | nil = … since the " | nil" union part makes this an “nullable”/"?" type.

When I then try to call REGameItems:FireServer(…) it gives this warning:
W000: (209,17) Type ‘RemoteEvent?’ does not have key ‘FireServer’

I cannot remove the “| nil” from the type definition since I check if this event is nil.

How can keep the “| nil” in the type definition and also not get that “Type ‘RemoteEvent?’ does not have key ‘FireServer’” warning?

I know of one way around this, but it’s very ugly, which is to use the :: operator, but I cannot get it to work without adding an extra temporary variable because I get an error otherwise:
local x : RemoteEvent = REGameItems :: RemoteEvent
x:FireServer(GameItemRemotes.REMsg_BuyItem, RemoteDataPacket)

Don’t type check?
I personally haven’t found a use for it, so if you’ve got any I’d like to know of them.

Not having types in a big program is a severe sources of bugs, in my case I need to type check otherwise the compiler won’t catch errors due to multiple level of callbacks.

1 Like