How to properly decorate request in TypeScript #5100
-
|
I'm trying this code and it fails to compile and throws an error. Compile Error Fastify version: tsconfig.json {
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"rootDir": "./src",
"allowJs": false,
"outDir": "./dist",
"noEmitOnError": true,
"preserveConstEnums": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"useUnknownInCatchVariables": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"allowUnreachableCode": true,
"skipLibCheck": true,
"experimentalDecorators": true
},
"include": ["./src/**/*.ts"],
"exclude": ["node_modules"]
} |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 2 replies
-
|
Hey! Remember to do declare module 'fastify' {
interface FastifyRequest {
user: {
id: number
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
In my declare module 'fastify' {
interface FastifyRequest {
user: {
id: number;
};
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Attached my |
Beta Was this translation helpful? Give feedback.
-
|
@metcoder95 Thanks! But augmenting interface in "scripts": {
"build": "tsc ",
"start": "node ./dist/server.js",
"dev": "ts-node --files ./src/server.ts "
},I was able to figure this out when I directly tried building and it did not give any error at compile time. Again Thanks for your help. |
Beta Was this translation helpful? Give feedback.
-
|
create file fastify.d.ts in src/@types declare module "fastify" {
export interface FastifyInstance<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse
> {
blipp(): void; // declare here
}
} |
Beta Was this translation helpful? Give feedback.
-
|
How to make it work in the instance level? The accepted solution adds the type to all requests, even those that didn't use For example what they ask here: https://stackoverflow.com/questions/73056086/how-to-separately-type-fastify-decorators-in-different-plugins |
Beta Was this translation helpful? Give feedback.
-
|
I was 5 minutes away from scraping my Fastify app and switching back to Hono.js. I was just trying to add some context to my request and all the official documentation did was show me how to make things worse. The one post in this thread fixed my issue. The only reason to not use Hono is that the OpenAPI integration is horrible. |
Beta Was this translation helpful? Give feedback.
-
|
Hi 👋 Check out the official Fastify demo, it features a fully typed application with 100% test coverage: Also, I recently introduced a new way of working with decorators via the Here’s a working example: interface IUser {
name: string
}
server.register(async function (fastify) {
const usersRepository = fastify.getDecorator<PostgreUsersRepository>(
'usersRepository'
)
fastify.decorateRequest('user', null)
fastify.addHook('onRequest', async (req, reply) => {
req.setDecorator<IUser>('user', { name: 'Jean' })
})
fastify.get('/me', (request, reply) => {
return request.getDecorator<IUser>('user')
})
})Let me know if you have any questions or feedback! |
Beta Was this translation helpful? Give feedback.
You need to do the augmentation on the same place as the application code, otherwise, it rewrites the module type definition, leading to errors. A working example might be: