#Fastify #TypeScript
https://www.fastify.io/docs/latest/TypeScript/#using-generics
[[FastifyでTypeScript#ソースコード]]がある前提。
## インターフェースを定義
```ts
interface IParams {
id: string;
}
interface IQuerystring {
debug: boolean;
}
interface IHeaders {
// h-Customではダメ
"h-custom": string;
}
```
公式ドキュメントのサンプルコード通りに書くと、[[Fastifyのリクエストヘッダがすべて小文字になってしまう]]問題によって`IHeaders`の`h-Custom`が認識されないので注意。これを回避するため==`IHeaders`のkeyは小文字にしなければいけない==。
## エントリポイント処理の修正
`params`、`queryString`、`headers`を受けつけるようして、レスポンスで返却する。
```ts
server.get<{
Params: IParams;
Querystring: IQuerystring;
Headers: IHeaders;
}>("/ping/:id", async (request, reply) => {
return {
id: request.params.id,
debug: request.query.debug,
custom: request.headers["h-custom"],
};
});
```
## 動作確認
```bash
curl -s -H 'h-Custom: hoge' localhost:8080/ping/100?debug=true | jq .
{
"id": "100",
"debug": "true",
"custom": "hoge"
}
```