msw icon indicating copy to clipboard operation
msw copied to clipboard

Unable to use HttpResponse.error() with TypeScript

Open jacquesg opened this issue 1 year ago • 3 comments

Prerequisites

Environment check

  • [X] I'm using the latest msw version
  • [X] I'm using Node.js version 18 or higher

Node.js version

v20.12.1

Reproduction repository

https://codesandbox.io/p/sandbox/friendly-pare-8tkfgz?file=%2Fmocks%2Fhandlers.ts

Reproduction steps

npx tsc

Current behavior

Adding a return HttpResponse.error() results in a TS error:

mocks/handlers.ts:10:31 - error TS2345: Argument of type '() => Response' is not assignable to parameter of type 'GraphQLResponseResolver<GraphQLQuery, GraphQLVariables>'.
  Type 'Response' is not assignable to type 'AsyncResponseResolverReturnType<GraphQLResponseBody<GraphQLQuery>>'.
    Property '[bodyType]' is missing in type 'Response' but required in type 'StrictResponse<GraphQLResponseBody<GraphQLQuery>>'.

10   graphql.query('ListMovies', () => {
                                 ~~~~~~~

Expected behavior

No error.

jacquesg avatar Apr 11 '24 08:04 jacquesg

Hi, @jacquesg! Thanks for reporting this. This looks like a bug and we should fix it.

Do you mind writing a simple type test for this? Putting it somewhere in this test suite would be the best. You can then open a PR with a failing test and we can investigate it from there. Let me know if that sounds interesting to you!

kettanaito avatar Apr 12 '24 02:04 kettanaito

Hi @kettanaito, I've submitted PR #2132.

jacquesg avatar Apr 13 '24 21:04 jacquesg

I shared a solution and my concerns with it in the PR.

kettanaito avatar May 08 '24 15:05 kettanaito