Skip to content
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

Typescript error: StrictResponse of generic is not assignable to AsyncResponseResolverReturnType #1823

Closed
4 tasks done
gustavhagland opened this issue Nov 1, 2023 · 3 comments · Fixed by #1824
Closed
4 tasks done
Assignees
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser

Comments

@gustavhagland
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 14 or higher

Browsers

No response

Reproduction repository

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgCRjMAlApgZ0gOxywBo4ARLAMwEMBXAGxgCEIATATwBV2wS4ALNGDgBfOJSgQQcAOQgcAdxkBuAFDrKtfAGMYwCPjgQA1gHEsMZNXyt6WKAB5O2PAaJwsADxhYbOOAByBgEM9NQARnYOFDQMzGxcPFgAfMkAFL6sAAoQwPgwAFxwODBQeQDmAJSIqnBwUBa0UIZpDa6EWEXOuARE1QC8yTV1dYLoAHTlFhk2OXkwpGkDQ6joLr1Y4wBWOAatPW5YlZW1onCqIuoA9FdwAKKe1OB2cLQ41FOq2m7wXk9gdgAshBtMYrDY7FA4P0jGYLODbPYHAhqGgyuFaD4ivhaCBwvYROkAERXP7PLCkmyQeZEk5fH4CayIqD+GEAbVOZIBWGBoIRkLSKLRwAxWLgAEYRCcALpAA

Reproduction steps

I'm trying to create wrappers around the http.get method to be able to couple endpoints in our backend with their return types. Here is an example of one such wrapper:

import { HttpResponse, DefaultBodyType, http } from 'msw';

function okGetHandler<TResponse extends NonNullable<DefaultBodyType>>(endPoint: string) {
  return (response: TResponse) => {
    http.get(endPoint, () => HttpResponse.json(response))
  } 
}

Current behavior

This causes a Typescript error:
Type 'StrictResponse' is not assignable to type 'AsyncResponseResolverReturnType'.
Type 'StrictResponse' is not assignable to type 'TResponse extends undefined ? Response : StrictResponse'

Expected behavior

I expect StrictResponse to be assignable to AsyncResponseResolverReturnType.

There is a easy workaround which is to simply use the native Response like so:

export function okGetHandler<TResponse extends NonNullable<DefaultBodyType>>(endPoint: string) {
    return (response: TResponse) =>
        http.get(
            url + endPoint,
            () => new Response(JSON.stringify(response), { headers: { 'Content-Type': 'application/json' } }),
        )
}

this is not as readable however.

Is there some extends clause that I'm missing on TResponse?

@gustavhagland gustavhagland added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Nov 1, 2023
@kettanaito kettanaito self-assigned this Nov 1, 2023
@kettanaito
Copy link
Member

Thanks for reporting this, @gustavhagland! I looked into this and discovered that this was indeed a bug in MSW. Opened a fix in #1824. Your example compiles without errors with that fix. Also take a look at how I put your example into a regression test and see if you'd get inspired from it in any way: https://github.com/mswjs/msw/pull/1824/files#diff-75a5caa27c79c5fbb266f10a9c4348c50f06e36ba284099859c04688ec183ddd

@gustavhagland
Copy link
Author

Thank you for the quick response! That looks great!

@kettanaito
Copy link
Member

Released: v2.0.3 🎉

This has been released in v2.0.3!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants