After upgrading to 0.21.0 typescript in strict mode is complaining when registering handlers to setupServer or setupWorker.
Tried it both using the new RequestBodyType and without.
msw: 0.21.0nodejs: 12.18.3npm: 6.14.6Steps to reproduce the behavior:
No typing errors
I think this is due to the unknown types on the RequestHandler.
I got it working in the reproduction by changing the following
- export declare type RequestHandlersList = RequestHandler<any, any>[];
+ export declare type RequestHandlersList = RequestHandler<any, any, any, any, any>[];
or
- export interface RequestHandler<RequestType = MockedRequest, ContextType = typeof defaultContext, ParsedRequest = unknown, PublicRequest = RequestType, ResponseBodyType = unknown>
+ export interface RequestHandler<RequestType = MockedRequest, ContextType = typeof defaultContext, ParsedRequest = any, PublicRequest = RequestType, ResponseBodyType = any>
I didn't verify the impact in the msw repository itself...
@timdeschryver can confirm that changing unknown to any for RequestHandler fixes the error. I'd vote for changing RequestHandler over RequestHandlersList, in case anything new implements RequestHandler, at least it won't require you to set explicit any in the generics.
I'm also having TS errors after upgrading to 0.21.0.
I saw that you updated TS to v4, which is not compatible to TS v3. I would suggest to downgrade to TS v3 for now (I had also similar issues with another library).
Thanks
PS: the error I'm getting (in case it helps)
Argument of type 'RequestHandler<GraphQLMockedRequest<TAmILoggedInQueryVariables>, GraphQLMockedContext<TAmILoggedInQuery>, GraphQLRequestParsedResult<...>, GraphQLMockedRequest<...>, unknown>' is not assignable to parameter of type 'RequestHandler<any, any, unknown, any, unknown>'.
Types of property 'getPublicRequest' are incompatible.
Type '((req: GraphQLMockedRequest<TAmILoggedInQueryVariables>, parsedRequest: GraphQLRequestParsedResult<TAmILoggedInQueryVariables>) => GraphQLMockedRequest<...>) | undefined' is not assignable to type '((req: any, parsedRequest: unknown) => any) | undefined'.
Type '(req: GraphQLMockedRequest<TAmILoggedInQueryVariables>, parsedRequest: GraphQLRequestParsedResult<TAmILoggedInQueryVariables>) => GraphQLMockedRequest<...>' is not assignable to type '(req: any, parsedRequest: unknown) => any'.
Types of parameters 'parsedRequest' and 'parsedRequest' are incompatible.
Type 'unknown' is not assignable to type 'GraphQLRequestParsedResult<TAmILoggedInQueryVariables>'.
Hello, everybody. Thank you for raising this, and an extra thanks to @timdeschryver for the suggested solution.
I've prepared a PR with the fix, could somebody please try it out and confirm it solves the issue? Thanks.
https://github.com/mswjs/msw/pull/379
Should be fixed in 0.21.1.
After pulling 0.21.1, I'm seeing these types of typing errors.
Type error: Argument of type 'ResponseTransformer<{ errors: Partial<GraphQLError>[]; }>' is not assignable to parameter of type 'ResponseTransformer<unknown>'.
137
Types of parameters 'res' and 'res' are incompatible.
138
Type 'MockedResponse<unknown>' is not assignable to type 'MockedResponse<{ errors: Partial<GraphQLError>[]; }>'.
139
Type 'unknown' is not assignable to type '{ errors: Partial<GraphQLError>[]; }'.
Having the same error as well in 0.21.1
Argument of type 'ResponseTransformer<{ errors: Partial<GraphQLError>[]; }>' is not assignable to parameter of type 'ResponseTransformer<unknown>'.
Types of parameters 'res' and 'res' are incompatible.
Type 'MockedResponse<unknown>' is not assignable to type 'MockedResponse<{ errors: Partial<GraphQLError>[]; }>'.
Type 'unknown' is not assignable to type '{ errors: Partial<GraphQLError>[]; }'.
70 res.once(ctx.errors([{ message: 'Unauthorized' }]))
@aldis-ameriks, @emmenko, thank you for the follow up! I've issued https://github.com/mswjs/msw/pull/382 where we drop all the unknown generics altogether. Could you see if that PR fixes your usage? Thanks.
@kettanaito with 0.21.2 the type errors are gone. Thanks!
PS: may I ask why you use any instead of unknown? Shouldn't we avoid the usage of any?
@emmenko glad to hear that it works nice.
The usage of any is preconditioned by multiple factors: first of all, certain generics should be provided by the end developer while remaining optional; other than that the response composition utilities are split into separate modules which makes it hard to inherit parental generics, as they are declared in different contexts. That's also vastly a question of familiarity with TypeScript, so we are always open to improvements and suggestions!