Conversation

I'm using TypeScript in an Express app. I want to write middleware that authenticates the user and sticks them in req.user... but only on routes that require being logged in. But the type of `Request` doesn't have `user` in it. What do I do? (See non-solutions in the next tweet.)
5
27
This is what I do now. But you didn't actually show what this does to authentication. This is what I have in practice, in every single route: const user = await findUser(req) if (!user) { res.redirect(paths.login()) return } > 50% of my route code is this.
1
4
I don't see any way around this, because there's no way for the `findUser` method to abort the request early. I mean, it can `res.status`, but it can't force the calling route to terminate.
3
2
It’s definitely not advice! But maybe not a joke? Monads, or more generally delimited continuations, have the interesting property of being able to accumulate type information by virtue of each continuation being an overloaded function of the previous state to the next state.
2
3
As an aside, this is why I’m still a believer in “success typings”. You can’t model everything with types, so you shouldn’t be tempted to try. It’s kinda fun to watch you solve these puzzles though, because I suspect you will eventually rage quit contemporary type systems :)
2
4
Fair enough. My actual view is somewhat more nuanced. I don’t believe that erased types are workable either, which I bet is a big part of the problem in Erlang. I also want reflection, contract enforcement, and other runtime services.
1
2
Show replies