-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using strict
in a project tsconfig.json
makes requestContext.get(string)
possibly undefined
#93
Comments
strict
in a project tsconfig.json
makes requestContext.get(string) possibly undefined
strict
in a project tsconfig.json
makes requestContext.get(string)
possibly undefined
The typing is correct, https://github.com/kibertoad/asynchronous-local-storage/blob/4504e910c5c88f8f52f74fad046ed7fa2bc8cf63/lib/als.ts#L7-L15 |
Thanks for the quick response! So that sort of tells me that the docs should at least be updated then? https://github.com/fastify/fastify-request-context/blob/master/README.md where it says
maybe there could be a comment added that this is only the case when |
Would you like to send a Pull Request to improve the documentation? |
For other folks who run into this issue, I am sort of fixing this by redefining the type: declare module 'fastify-request-context' {
interface RequestContextData {
val: string;
val2?: string;
}
interface RequestContext {
get<K extends keyof RequestContextData>(key: K): RequestContextData[K];
}
} I think this is valid because I am setting correctly typed defaults: import fastify from 'fastify';
import { fastifyRequestContextPlugin } from 'fastify-request-context';
const server = fastify();
server.register(fastifyRequestContextPlugin, {
defaultStoreValues: {
val: '',
// Since `val2` is typed as possibly undefined, we can leave it undefined here.
},
}); If you decide to set |
Closing this issue now that the pull request has been merged and it is link to from the README. Please ping me on this thread though if anyone has a better idea of how to work around this issue. Thanks @climba03003 for the discussion. |
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
andfastify-request-context
, the user could get confused becauseand
value
is set tonumber | undefined
. In the documentation it states that thevalue
should 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
tsconfig.json
app.ts
a.ts
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 causeslogger
above ina.ts
to beFastifyLoggerInstance | undefined
. This is not the case if you add"strictNullChecks": false
to thetsconfig.json
.Expected Behavior
Could the
.get
method 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": true
and"strictNullChecks": false
.The text was updated successfully, but these errors were encountered: