From a0ec8be311daef0285d8485e315bbb49a9370f44 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Sat, 4 Nov 2023 16:21:08 +0100 Subject: [PATCH] fix: correctly check "undefined" as response body type (#1824) --- src/core/handlers/RequestHandler.ts | 4 ++-- test/typings/regressions/response-body-type.test-d.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 test/typings/regressions/response-body-type.test-d.ts diff --git a/src/core/handlers/RequestHandler.ts b/src/core/handlers/RequestHandler.ts index 8f45a6e41..da25a7662 100644 --- a/src/core/handlers/RequestHandler.ts +++ b/src/core/handlers/RequestHandler.ts @@ -30,7 +30,7 @@ export interface RequestHandlerInternalInfo { export type ResponseResolverReturnType< BodyType extends DefaultBodyType = undefined, > = - | (BodyType extends undefined ? Response : StrictResponse) + | ([BodyType] extends [undefined] ? Response : StrictResponse) | undefined | void @@ -265,7 +265,7 @@ export abstract class RequestHandler< // Clone the previously stored response from the generator // so that it could be read again. - return this.resolverGeneratorResult.clone() + return this.resolverGeneratorResult.clone() as StrictResponse } if (!this.resolverGenerator) { diff --git a/test/typings/regressions/response-body-type.test-d.ts b/test/typings/regressions/response-body-type.test-d.ts new file mode 100644 index 000000000..2c462dd17 --- /dev/null +++ b/test/typings/regressions/response-body-type.test-d.ts @@ -0,0 +1,10 @@ +/** + * @see https://github.com/mswjs/msw/issues/1823 + */ +import { http, Path, HttpResponse, DefaultBodyType } from 'msw' + +function myHandler(path: Path) { + return (response: CustomResponseBodyType) => { + http.get(path, () => HttpResponse.json(response)) + } +}