我想将token: string
字段添加到FastifyContext接口。为此,我创建了以下文件结构:
projectDir
|__src
| |__@types
| | |__fastify
| | | |__index.d.ts
| |__api
| | |__authHook.ts
|__tsconfig.json
src/@types/fatify/index.d.ts
的内容:
declare module 'fastify' {
export interface FastifyContext<ContextConfig>{
token: string
}
}
src/api/authHook.ts
的含量
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;
}
tsconfig.json
的内容:
{
"compilerOptions": {
...
"typeRoots": ["src/@types"],
...
}
}
但是当我运行代码时,我得到以下错误:
Property 'token' does not exist on type 'FastifyContext<unknown>'.
我做错了什么?
1条答案
按热度按时间f5emj3cl1#