S
SolidJS•8mo ago
aryzing

Why is `BoundEventHandler` typed this way?

The type for BoundEventHander is,
interface BoundEventHandler<T, E extends Event> {
0: (
data: any,
e: E & {
currentTarget: T;
target: DOMElement;
}
) => void;
1: any;
}
interface BoundEventHandler<T, E extends Event> {
0: (
data: any,
e: E & {
currentTarget: T;
target: DOMElement;
}
) => void;
1: any;
}
In order to use it, I need to do something like const handler = BoundEventHandler<T,E>[0], note the [0] at the end, which seems weird. Why is it typed this way? How is this type expected to be used?
5 Replies
thetarnav
thetarnav•8mo ago
this is for the onClick={[callback, data]} syntax
aryzing
aryzing•8mo ago
So it's normal to use [0] when typing a bound event handler function? Since the interface has Handler in the name, it would seem that it is the type for a function, not a tuple
lxsmnsyc
lxsmnsyc•8mo ago
it is a tuple type, but it's declared this way since interfaces that are redeclared extend their own types whether or not it needs to be typed this way can be up for discussion though
aryzing
aryzing•8mo ago
Thanks @lxsmnsyc 🤖 . I guess I asked about the type because I was a bit surprised by the type syntax needed to type the handler, although perhaps a better question is how are handlers typically typed?
lxsmnsyc
lxsmnsyc•8mo ago
it's either just the plain callback or that it's a union, so to speak
Want results from more Discord servers?
Add your server