I need to incorporate the token: string
field into the FastifyContext interface. To achieve this, I have set up the following file structure:
projectDir
|__src
| |__@types
| | |__fastify
| | | |__index.d.ts
| |__api
| | |__authHook.ts
|__tsconfig.json
The contents of src/@types/fastify/index.d.ts
are as follows:
declare module 'fastify' {
export interface FastifyContext<ContextConfig>{
token: string
}
}
The contents of src/api/authHook.ts
include:
import {FastifyRequest, FastifyReply} from "fastify"
export default async function authHook(request: FastifyRequest, reply: FastifyReply): Promise<void>{
// Some logic
const token = "example_token" // some result from logic
request.context.token = token;
}
The contents of tsconfig.json
are detailed below:
{
"compilerOptions": {
...
"typeRoots": ["src/@types"],
...
}
}
However, upon running the code, I encounter the following error:
Property 'token' does not exist on type 'FastifyContext<unknown>'.
What could be causing this issue?