Closed jasonaowen closed 2 years ago
I'm not a TypeScript expert or even active user so my ability to help with this will be very limited. My hunch is that, since this is a run-time conversation from string to integer, the TS compiler doesn't have a way to know that req.params.id
has been changed to an integer. You may have to recast the value before calling f()
I realize this isn't a great solution because now you're going to have to re-cast every result coming out of celebrate
which is far from ideal.
One possible less-annoying path would be to create your own middleware that you'd insert right after celebrate
calls. In there, you could either construct a new object and attach it to res.locals
for example and then use that in the rest of the route. That way you could type it as needed. You could also recast req.*
in there as well before calling next()
Hopefully someone can weigh in with a better idea than the one I've had.
Here-in lies the problem with trying to formally type a loosely typed language 😅
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
node
version -v12.22.5
, via Debian bullseye packagenodejs 12.22.5~dfsg-2~11u1
celebrate
version -celebrate@15.0.1
joi
version (vianpm ls --depth=0 | grep joi
) - (none - using version provided by Celebrate, which isjoi@17.6.0
)How do you recommend using request data that Celebrate/Joi has converted as part of validation? The type definitions for express (
@types/express@4.17.13
) define parameters asstring
s (seeParamsDictionary
type definition). With conversions turned on (as they are by default, and as I want Celebrate & Joi to do), those type definitions no longer match the actual data, and TypeScript rejects otherwise-correct code.Here is an example value that is not working as expected:
When queried, it returns the expected data:
But uncommenting line 16 results in a compilation error:
By the way, thank you for making and maintaining Celebrate! Using it has been delightful.