Problem
Currently, all implementations of typed signal have their own problems. Let’s look into the most popular typing methods of signals in order to see this problems.
Sleitnick’s Typed Remote (not a signal actually, but the problem is the same)
type Signal<T...> = {
Connect: (self: Signal<T...>, fn: (T...) -> ()) -> RBXScriptConnection,
ConnectParallel: (self: Signal<T...>, fn: (T...) -> ()) -> RBXScriptConnection,
Once: (self: Signal<T...>, fn: (T...) -> ()) -> RBXScriptConnection,
Wait: (self: Signal<T...>) -> T...,
}
export type Event<T...> = Instance & {
OnClientEvent: Signal<T...>,
OnServerEvent: PlayerSignal<T...>,
FireClient: (self: Event<T...>, player: Player, T...) -> (),
FireAllClients: (self: Event<T...>, T...) -> (),
FireServer: (self: Event<T...>, T...) -> (),
}
- All semantics are lost (we don’t know what we logically should use in
:Fire(), we don’t know what purpose have values that would be given to function that we connect using:Connect(), we only see types.
Standard method of typing signal (if you just take GoodSignal or any other and don’t add typing inside of it)
It looks like this:
export type Signal = {
Connect: (self: Signal, callback: (...any) -> ()) -> Connection,
Once: (self: Signal, callback: (...any) -> ()) -> Connection,
DisconnectAll: (self: Signal) -> (),
Wait: (self: Signal) -> ...any,
Fire: (self: Signal, ...any) -> ()
}
- Same as before, all semantics are lost.
- We don’t even have hints what types should variables have when we pass arguments to
:Fire()or when we connect function using:Connect()
Wish
As we can see, when we try to use any of Roblox’s RBXScriptSignal we have a hint: what arguments should our function that is connected have and also we see meaning of this arguments:
Solutions
Key idea: let’s pass a type of function as a generic argument!
Weak Ronaldo
export type RonaldoSignal<T> = {
Fire: (self: RonaldoSignal<T>, ...any) -> (),
Connect: (self: RonaldoSignal<T>, func: T) -> Connection,
Once: (self: RonaldoSignal<T>, func: T) -> Connection,
DisconnectAll: (self: RonaldoSignal<T>) -> (),
Wait: (self: RonaldoSignal<T>) -> ...any
}
As you can see, now are able to typecheck function which is being connected. Also we can see meaning of arguments (like Roblox does for us):
Unfortunately, there are some cons:
- We don’t have typechecking in
:Fire()and we don’t have any meaning of arguments that we should pass - We don’t have typechecking in
:Wait()meaning that if it returned anything, we don’t know types of returned data
As you can see, I was able to easily pass into :Fire() some invalid numbers that are not related to function we connect
Here we can apply another key idea: let’s make Fire be called with a . instead of :!
Impatient Ronaldo
export type RonaldoSignal<T> = {
Fire: T,
Connect: (self: RonaldoSignal<T>, func: T) -> Connection,
Once: (self: RonaldoSignal<T>, func: T) -> Connection,
DisconnectAll: (self: RonaldoSignal<T>) -> (),
Wait: (self: RonaldoSignal<T>) -> ...any
}
We can do it 2 ways:
- create local function
Fireinside of.new()and setself.Fire = Fire. With that we would be able to useselfin implementation ofFireand we also catch a call ofFire, so metatable will not be indexed - change implementation of metamethod
__indexand make local functionFirefor the whole script. In metamethod we will call this function since from__indexwe have a table which was used for a call ofFire
Now we get typechecking even on .Fire!
Unfortunately, our Impatient Ronaldo is not able to get typing on :Wait() and will always think that ...any is being returned. If you want, you can remove method :Wait() and already get full typechecking.
Final idea: let’s add variadic argument!
Strong Ronaldo
With that we are able to add typing even when we use :Wait()!
The only con of Strong Ronaldo is that now we should duplicate types of arguments:
- First time we write them when we describe arguments of functions which should be connected
- Second time we write them to be able to fix
:Wait()return
But now we get fully typed signal!
With that approach we can also get typing for RemoteEvents and RemoteFunctions that will make our life easier when we do any networking.
Hope it helps!
(P.S: code which is provided on Pastebin is just an example of how you can do it and in fact it is just a GoodSignal with added typing. Main idea of this topic is to explain how you can get typing for signal that you currently use)









