-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: let ctx.session
be undefined for missing session keys
#5
Comments
ctx.session
be undefined for missing sessionsctx.session
be undefined for missing session keys
We should probably still throw an error upon assignment to |
I'm guessing the type of |
I think so, too. It would be really annoying to work with. Then again ... we either have to accept that the types are wrong ( |
I get that it could be kind of annoying, but with filtering it'd be easy to get rid of updates missing the session object. We could even export a predicate ourselves, especially after we move sessions to a dedicated plugin |
What are you envisioning? |
yep, something like that |
That feels a bit annoying, don't you think? |
not really. the runtime check is already necessary for stuff that depend on the existence of a session key, well just reflect that in the types. |
I'd just like to find a way to make it unnecessary to check things at runtime. The current implementation doesn't need that and it would feel like a step back to require this from now on |
I might be missing something here, but I don't really get how this is true. isn't the whole purpose of this PQ to make the check easier? today, if you don't perform that runtime check, you risk getting an error |
I don't think you're missing something. It's just really annoying to be forced to check for
|
I honestly think the most ergonomic choice is 1. It's true to the nature of the It's the least footgunish of the three approaches IMO. |
Most ergonomic? ctx.session.count++ becomes (ctx.session ??= { prop: 0 }).prop++ or less cryptic ctx.session ??= { prop: 0 }
ctx.session.prop++ and in both cases, 100 % of the added code is just dead code. There is no code path that actually causes the session to be initialised here. It's purely an addition to satisfy TypeScript. Also, note that the initialiser needs to list every single field in the session. Developers either need to make all fields optional and then add a second layer of checking, or they might resort to calling This needs to be done in every single place anything is done with the session. The migration efforts are huge. There may be reasons to go with 1 but being ergonomic is not among them :D |
I meant it's ergonomic because you can eliminate all that with a very simple Maybe it's not right to say it's the more ergonomic, but then it's the most correct, while still allowing easy checking. I hate the idea of the types telling me To be fair, with any of the approaches, we still have to check every time we use it, otherwise we might get "cannot read [...] of undefined" errors, or get an exception thrown at us. I don't really see why having a property that's possibly undefined and easy to filter away is bad. It goes along every other optional property in Context, actually. You'll always have to narrow tb context type down, this is not very different from it. The best I can think of in terms of DX and type safety is:
Usage options would be:
bot.filter(hasSession)
.message((ctx, next) => {
ctx.session.count++
return next()
})
bot.message((ctx, next) => {
if (!ctx.hasSession) {
return ctx.sendMessage('Hi nameless')
}
ctx.session.count++
return next()
}) We could maybe even drop 1 and stick with 2, letting people make their own predicates, depending on whether they want to handle rh existence or absence of a session. My point is: it's better DX to require the user to filter this out and then use the property safely then to let it the property return undefined or throw without the types telling the user that's possible. |
The fact that an error is thrown upon property access is really annoying. There is no good way of knowing whether session data exists, so we should change this behaviour.
The text was updated successfully, but these errors were encountered: