Export types
Closed this issue · 6 comments
I would like to use the interfaces defined in express-joi-validation.d.ts
.
Would be great if these were exported, even at the cost of a breaking change.
@dhessler can you provide an example of your desired use?
@evanshortiss you extended the express Request interface but did not export it. Which in effect creates such errors:
import { Request, Response, Router } from 'express'
function handle(req: Request, res: Response) {}
Router().get('/', handle)
// ^^^ error, `Request` not compatible with new extended Request
@shilangyu @dhessler can you take a look at the 2.0 branch? It improves typings.
Here's the example usage. Note it uses a ValidatedRequest
type, that extends express.Request
:
import * as Joi from '@hapi/joi'
import {
ValidatedRequest,
ValidatedRequestSchema,
createValidator
} from 'express-joi-validation'
import { Router } from 'express'
import 'joi-extract-type'
const route = Router()
const validator = createValidator()
const querySchema = Joi.object({
name: Joi.string().required()
})
interface HelloRequestSchema extends ValidatedRequestSchema {
query: Joi.extractType<typeof querySchema>
// You can add body, headers, etc. here
}
route.get(
'/hello',
validator.query(querySchema),
(req: ValidatedRequest<HelloRequestSchema>, res) => {
// req.query types are matching the HelloRequestSchema!
res.end(`Hello ${req.query.name}`)
}
)
@shilangyu for your example, does using express.Request
make a difference? I didn't get that error before, but will see can I recreate it using current master branch.
Yes 2.0 + joi-extract-type
seems to be fixing typing issues ive been having. Looks great! Cant wait for the release!
@shilangyu fantastic 😄 I'll make a release later.
@shilangyu released as 2.0.0, please open an Issue if you have any trouble 👍