Skip to content

Infer resolver's response body type from return type of given fetcher function #1171

Answered by kettanaito
bennettdams asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, @bennettdams. Thanks for raising this.

I believe you can achieve what you want simply by providing the MyData type as the ResponseBodyType generic to REST API request handlers:

interface MyData {
  username: string
}

function fetcher(): Promise<MyData> {}

rest.post<never, MyData>('/resource', (req, res, ctx) => {
  return res(ctx.json({ username: 'octocat' }))
})

Also note that the generic signature is RequestBodyType, ResponseBodyType:

msw/src/rest.ts

Lines 12 to 16 in c30bcd5

return <
RequestBodyType extends DefaultBodyType = DefaultBodyType,
ResponseBodyType extends DefaultBodyType = DefaultBodyType,
PathType extends Path = Path,
>(

The thir…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@bennettdams
Comment options

@kettanaito
Comment options

@bennettdams
Comment options

@kettanaito
Comment options

Answer selected by bennettdams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants