Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoteEvent / BindableEvent / RemoteFunction / BindableFunction should be "unknown?" instead of "any" #799

Open
nightcycle opened this issue Oct 20, 2024 · 1 comment

Comments

@nightcycle
Copy link

nightcycle commented Oct 20, 2024

As the firing of these Roblox instances have no way to enforce type safety, I feel it's cleanest to make developers validate the types that are received.

In this below example, no errors are detected - everything is fine. That player variable could be passed into another function and cause a really weird error somewhere far away from the event.

local bindableEvent: BindableEvent

bindableEvent.Event:Connect(function(player: Player)
  -- the player is actually a string, but the dev is expecting a player
end)

bindableEvent:Fire(Players.LocalPlayer.Name)

What I propose is that it's better for the code to accept this uncertainty, and refine it in the following code. For example:

local bindableEvent: BindableEvent

bindableEvent.Event:Connect(function(player: unknown?)
  assert(typeof(player) == "Instance", `player is not instance, got type "{typeof(player)}"`)
  assert(player:IsA("Player"), `player is not player, got class "{player.ClassName}"`)
  -- the type is now certainly a player, or has given the developer a clear error at the earliest point possible
end)

bindableEvent:Fire(Players.LocalPlayer.Name)

I feel this handling of it is safe and more accurate to what's actually happening in the code, similar to the improvement made to attributes earlier.

Thank you for your time!

@nightcycle nightcycle changed the title RemoteEvent / BindableEvent / RemoteFunction / BindableFunction should be "unknown" instead of "any" RemoteEvent / BindableEvent / RemoteFunction / BindableFunction should be "unknown?" instead of "any" Oct 20, 2024
@JohnnyMorganz
Copy link
Owner

The main reason I am hesistant about this is from a user experience PoV, as switching this from any to unknown will lead to loads of type errors throughout existing code bases I imagine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants