-
Notifications
You must be signed in to change notification settings - Fork 254
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
Allow OnErrorCallbacks to return Promise<boolean> #1224
Conversation
77a99b7
to
f4f291b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good, but since we're in this area I checked the node-style callback for correctness too and found it was also incorrect.
It's hard to get the typing strict enough that it doesn't let people write invalid sync/async without preventing people from writing valid node-style callbacks. I had a quick go and got close, but the never
in this example makes it unusable for non-trivial node-style callbacks…
From some more testing, it looks like we can't make it perfectly strict because of how Because of |
7328075
to
2ca2f5f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternatives are to either make it a union type, which makes type OnErrorCallback
= ((event: Event) => void | boolean)
| ((event: Event) => Promise<void | boolean>)
| ((event: Event, cb: (err: null | Error, shouldSend?: boolean) => void) => void)
notify(event => event.stuff) // error TS7006: Parameter 'event' implicitly has an 'any' type. or mark type OnErrorCallback = (event: Event, cb: (err: null | Error, shouldSend?: boolean) => void) => void | boolean | Promise<void | boolean>
notify(event => event.stuff) // works
notify((event, cb) => { cb(null) }) // works
notify(() => 1) // error TS2322: Type 'number' is not assignable to type 'boolean | void | Promise<boolean | void>'. |
I'd go with this. Marking it as always present doesn't mean people have to define it. |
This allows returning/resolving false in an async callback to prevent errors from sending. Previously this would cause a Typescript compile error
These were missing the 'shouldSend' parameter, so couldn't prevent sending
bc22b89
to
2ad9e21
Compare
Goal
Currently an async
OnErrorCallback
can only returnPromise<void>
in the Typescript, which means they can't be used to prevent events from sending without causing a compile error. Returning/resolving a boolean value does work, so we can safely allowPromise<boolean>
as a return type