Open CMCDragonkai opened 1 year ago
This is what it looks like when we have to add specific event types:
quicServer.addEventListener(
events.EventQUICServerConnection.name,
// @ts-ignore
(evt: events.EventQUICServerConnection) => {
const connection = evt.detail;
connection.addEventListener(
events.EventQUICConnectionStream.name,
// @ts-ignore
async (evt: events.EventQUICConnectionStream) => {
const stream = evt.detail;
// Graceful close of writable
process.stderr.write('>>>>>>>>> HANDLING THE QUIC SERVER STREAM\n');
await stream.writable.close();
// Consume until graceful close of readable
for await (const _ of stream.readable) {
// Do nothing, only consume
}
process.stderr.write('<<<<<<<< HANDLED THE QUIC SERVER STREAM\n');
}
);
}
);
It doesn't happen when those event handlers are defined elsewhere, but this can be annoying. If the Class.name
could be made into literal types would be even easier to do...
This would be useful https://github.com/microsoft/TypeScript/issues/1579.
Specification
Sometimes we get some weird type error about the fact that we cannot specify a more specific event type in our event handlers because event target isn't typed and thus allows any event to be dispatched to any event name.
To get around this we can extend our
Evented
interface to actually be typesafe and to receive an optionalEventMap
that would allow downstream classes to specify a typed map of events that is being used.This can be propagated to the
js-async-init
too, and it would be possible to do things like:It would be a breaking change on the types, but we don't really use the
StartReturn
andStopReturn
types much. And actually all the generic types would be optional here. TheEventMap
can be first since it is just likely to be used more.Anyway this is what we would do to
Evented
to achieve this:The above is pseudo code generated by chatgpt. What's cool is that it preserves the ability to not bother specifying specific types if you don't want to. You can still get the original behaviour of event map not caring about the type of the event. When you do care, you give it a much more specific type.
The relevant type map can then look like this:
One might be careful that
EventQUICConnectionStart.name
is of typestring
and not the literal type ofEventQUICConnectionStart.name
. So I'm not sure how TS will end up inferring it or not. If not, then you have to doaddEventListener<'EventQUICConnectionStart'>(...)
.A little more boilerplate.
Additional Context
Tasks
Evented
js-async-init
before enabling this feature@ts-ignore
.