I am moving my app from js to ts and encountered the following problem:
In my app I use a middleware function to validate requests. It checks stuff like whether req.files.image
exists, image type and image size
imageRouter.route('/upload').post(validateUploadImageData, uploadImage)
However, when I move to the next function uploadImage
, I notice a typescript error 'req.files' is possibly 'null' or 'undefined'.ts(18049)
.
I understand why it's happening: ts doesn't know anything about my validations in the other function, it just strictly checks the type in the current function. However, is there a workaround this problem other than doing the same checks and type narrowing in every middleware function?