fastify / fastify/help

Typescript: use request schema and reply type at the same time

Open
#822 1 comment 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
No language data
Stars
68
Forks
8
Avg merge
11h 2m
Merged PRs (30d)
2

Description

## 💬 Question here

I'm using yup integration to validate requests (it's not important here). But I also want to provide response type purely on the type level without a custom serializer like so:

```ts
server.get<{
Reply: string
}>('/', {
schema: {
querystring: object({
a: string().required(),
}),
},
}, async (req, res) => {
const a = req.query.a
return res.send('123')
})
```

But i'm getting an error `TS18046: 'req.query' is of type 'unknown'.`. Apparently the schema gets overridden by the generic type.

How do I fix this issue?

## Your Environment

- *node*: 18
- *fastify version*: ^4.12.0
- *os*: Mac
- *typescript*: 4.9.4

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.