How to properly type handlers response when using Typescript? #1503
|
Hi, I'm using I've searched for examples but couldn't find any with proper typing and the ones I've found were with older versions of I have this handler: export const journeyHandlers: RequestHandler[] = [
rest.get<DefaultBodyType, ResponseResolver<MarketGroup[]>>(
`${backendUrl}/api/journeys/:operatorCode/:journeyNumber/:journeyDepartureNumber/market-groups`,
(req, res, ctx) => res(ctx.status(200), ctx.json(marketGroupsMock))
),
rest.get<DefaultBodyType, ResponseResolver<Journey>>(
`${backendUrl}/api/journeys/:operatorCode/:journeyNumber/:journeyDepartureNumber`,
(req, res, ctx) => res(ctx.status(200), ctx.json(journeyMock))
),
];However, it doesn't matter what I put in How should these handlers be properly typed so if I either change the typing or the returned object I get an error? Thank you |
Replies: 1 comment 1 reply
|
Hi, @alveshelio. Thanks for raising this. If you take a look at the generics to
Lines 13 to 17 in 7380011 If you wish to annotate your handlers, respect that order: rest.get<DefaultBodyType, never, Journey>(url, resolver)
Let me know if this works for you. |
Hi, @alveshelio. Thanks for raising this.
If you take a look at the generics to
rest.*methods, you will see that they are listing things in the following order:msw/src/rest.ts
Lines 13 to 17 in 7380011
If you wish to annotate your handlers, respect that order:
Let me…