-
Notifications
You must be signed in to change notification settings - Fork 49
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 null Session in SessionCallback when there is an error. #180
base: master
Are you sure you want to change the base?
Conversation
Thanks @climba03003! I'm going to commit your suggestion and then would like to propose one more change to export the |
Co-authored-by: KaKa <[email protected]>
…d in store.get callback function.
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.
lgtm
@climba03003 PTAL |
Co-authored-by: KaKa <[email protected]>
@climba03003 this new type definition seems to not be working, or at least I don't understand how it should work. With the suggestion change, typescript tells me:
I am not quite sure how to solve this problem. Is seems this union type definition doesn't quite work for the use case. Any idea? I tried to change it to an overloaded function, but that caused the arguments in the callback function accepts to lose their types: |
CI is not happy |
Yeah it needs a little more work. The new tuple type seems to be causing issues as currently implemented. Will try to knock this out tomorrow. |
Hey folks 👋 I see this one didn't get finished and I'd like to have it fixed as well. I took some time to tinker around with Intersectiontype Callback = ( ((err: Error) => void) & ((err: null, result: number) => void) );
const getSession = (callback: Callback) => {
callback(new Error()); // correct
callback(new Error(), 5); // throws error, correct
callback(null, 5); // correct
}
getSession((error) => {
// parameters properly typed, TS compiles
return;
})
getSession((error, result) => {
// parameters properly typed, but TS does not compile
return;
}) Function overloadtype Callback = {
(err: Error): void;
(err: null, result: number): void;
}
const getSession = (callback: Callback) => {
callback(new Error()); // correct
callback(new Error(), 5); // throws error, correct
callback(null, 5); // correct
}
getSession((error) => {
// parameters properly typed, TS compiles
return;
})
getSession((error, result) => {
// parameters properly typed, but TS does not compile
return;
}) You can see within the comments that those do not work as intended due to how TypeScript works. I think that the most optimal way we could go is this: type Callback = {
(err: Error, result?: undefined): void;
(err: null, result: number): void;
}
const getSession = (callback: Callback) => {
callback(new Error()); // correct
callback(new Error(), 5); // throws error, correct
callback(null, 5); // correct
}
getSession((error) => {
// parameters properly typed but are not fully precise, TS compiles
return;
})
getSession((error, result) => {
// parameters properly typed but are not fully precise, TS compiles
return;
}) This way we lose the preciseness of the arguments of the callback that we pass to the @kylerush Let me know if you have time to finish this off. If not, please let me know - I'll create a separate PR with the fix. Also, let me know what you all folks think! |
@sarneeh go ahead and open a fresh PR! |
It looks like the existing code support an error being thrown on
store.get
, however, the type definitionSessionCallback
for the callback function onstore.get
requires aSession
type variable for the session argument. This change would not cause the type check to fail when you call thestore.get
callback function like so:This change also exports the
SessionCallback
type on theFastifySession
type so that we can specify that ourstore.get
callback function is of typeFastifySession.SessionCallback
.Checklist
npm run test
andnpm run benchmark
and the Code of conduct