Type error using default RequestHandler type
#498
-
|
Hey guys, I've set up Everything is working flawlessly but I have a TypeScript error that I can't get rid off without using Here is my code: And this is the TypeScript error that I get: By overcharging with Is there a way to achieve this without Thanks for your time 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 20 replies
-
|
Hi @Erazihel thanks for reaching us :). You can do in this way import {
rest,
RequestHandler,
MockedRequest,
RequestParams,
restContext,
ParsedRestRequest,
} from 'msw'
export function handleCourse(): RequestHandler<
MockedRequest<null, RequestParams>,
typeof restContext,
ParsedRestRequest,
MockedRequest<null>,
string
> {
return rest.get(COURSE_URL, (_, res, ctx) => res(ctx.json({ foo: 'bar' })))
}You can also drop the response type because it will be inferred from the return value export function handleCourse() {
return rest.get(COURSE_URL, (_, res, ctx) => res(ctx.json({ foo: 'bar' })))
} |
Beta Was this translation helpful? Give feedback.
-
|
I wonder if return type inference would be preferable in this scenario. |
Beta Was this translation helpful? Give feedback.
-
|
Just mentioning that four years later, I'm running into a bizarre issue where, if I have this file: In a brand new project this is fine, but in my existing project I see this error: Both are using MSW 2.8.0 No idea what this is about. Some other type has been overiden somewhere? |
Beta Was this translation helpful? Give feedback.
Hi @Erazihel thanks for reaching us :).
You can do in this way
You can also drop the response type because it will be inferred from the return value