Prerequisites
Fastify version
3.25.0
Plugin version
2.2.0
Node.js version
16.x
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.0.1 Monterey
Description
With a project using fastify and fastify-request-context, the user could get confused because
declare module 'fastify-request-context' {
interface RequestContextData {
a: number;
}
}
const value = request.requestContext.get('string');
and value is set to number | undefined. In the documentation it states that the value should be set to a type of number, but this is not always the case.
Steps to Reproduce
Create a new folder/nodejs project with these files.
package.json
{
"name": "fastify-request-context-typescript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fastify": "^3.25.0",
"fastify-request-context": "^2.2.0"
},
"devDependencies": {
"@types/node": "^17.0.0"
}
}
tsconfig.json
{
"compilerOptions": {
"strict": true // the cause of the issue
}
}
app.ts
import fastify, { FastifyLoggerInstance } from 'fastify';
import { fastifyRequestContextPlugin } from 'fastify-request-context';
import { simpleController } from './a';
declare module 'fastify-request-context' {
interface RequestContextData {
logger: FastifyLoggerInstance;
}
}
const server = fastify();
server.register((app) => {
app.get('/', {
handler: simpleController
});
}, {
prefix: '/helloworld',
})
server.register(fastifyRequestContextPlugin, {
defaultStoreValues: {
logger: server.log,
},
});
a.ts
import { RouteHandler } from 'fastify';
export const simpleController: RouteHandler = async (req, reply) => {
const logger = req.requestContext.get('logger');
// logger is possibly undefined, so this is a typescript error
logger.info('do a thing');
reply.code(204);
};
The issue is due to this typing I think:
https://github.com/fastify/fastify-request-context/blob/master/index.d.ts#L8
Since the type is RequestContextData[K] | undefined, in strict typing mode I think that this causes logger above in a.ts to be FastifyLoggerInstance | undefined. This is not the case if you add "strictNullChecks": false to the tsconfig.json.
Expected Behavior
Could the .get method just be typed as get<K extends keyof RequestContextData>(key: K): RequestContextData[K]? This seems to work with "strictNullChecks" set to either true (implicitly by "strict": true), or with "strict": true and "strictNullChecks": false.
Prerequisites
Fastify version
3.25.0
Plugin version
2.2.0
Node.js version
16.x
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.0.1 Monterey
Description
With a project using
fastifyandfastify-request-context, the user could get confused becauseand
valueis set tonumber | undefined. In the documentation it states that thevalueshould be set to a type ofnumber, but this is not always the case.Steps to Reproduce
Create a new folder/nodejs project with these files.
package.json{ "name": "fastify-request-context-typescript", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "fastify": "^3.25.0", "fastify-request-context": "^2.2.0" }, "devDependencies": { "@types/node": "^17.0.0" } }tsconfig.json{ "compilerOptions": { "strict": true // the cause of the issue } }app.tsa.tsThe issue is due to this typing I think:
https://github.com/fastify/fastify-request-context/blob/master/index.d.ts#L8
Since the type is
RequestContextData[K] | undefined, in strict typing mode I think that this causesloggerabove ina.tsto beFastifyLoggerInstance | undefined. This is not the case if you add"strictNullChecks": falseto thetsconfig.json.Expected Behavior
Could the
.getmethod just be typed asget<K extends keyof RequestContextData>(key: K): RequestContextData[K]? This seems to work with"strictNullChecks"set to eithertrue(implicitly by"strict": true), or with"strict": trueand"strictNullChecks": false.