Skip to content

Commit bb1faf8

Browse files
authored
fix(HttpResponse): set the default bodyType to any (#2439)
1 parent 5e042cb commit bb1faf8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/core/HttpResponse.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
normalizeResponseInit,
77
} from './utils/HttpResponse/decorators'
88

9+
const bodyType = Symbol('bodyType')
10+
911
export interface HttpResponseInit extends ResponseInit {
1012
type?: ResponseType
1113
}
1214

13-
declare const bodyType: unique symbol
14-
1515
export interface StrictRequest<BodyType extends DefaultBodyType>
1616
extends Request {
1717
json(): Promise<BodyType>
@@ -37,6 +37,8 @@ export interface StrictResponse<BodyType extends DefaultBodyType>
3737
* @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}
3838
*/
3939
export class HttpResponse extends FetchResponse {
40+
readonly [bodyType]: any
41+
4042
constructor(body?: BodyInit | null, init?: HttpResponseInit) {
4143
const responseInit = normalizeResponseInit(init)
4244
super(body, responseInit)

test/typings/http.test-d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,15 @@ it('errors when returning non-Response data from resolver', () => {
235235
() => ({}),
236236
)
237237
})
238+
239+
it('treats non-typed HttpResponse body type as matching', () => {
240+
http.get<never, never, { id: string }>('/resource', () => {
241+
/**
242+
* @note When constructing a Response/HttpResponse instance,
243+
* its body type must effectively be treated as `any`. You
244+
* cannot provide or infer a narrower type because these classes
245+
* operate on streams or strings, none of which are type-safe.
246+
*/
247+
return new HttpResponse(null, { status: 500 })
248+
})
249+
})

0 commit comments

Comments
 (0)