Skip to content

Commit

Permalink
test: add extra keys response body type
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 25, 2024
1 parent e5fa36e commit 228c1b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions test/typings/custom-resolver.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ it('custom http resolver has correct parameters type', () => {

http.get<{ id: string }, never, 'hello'>(
'/user/:id',
// @ts-expect-error Response body doesn't match the response type.
withDelay(250, ({ params }) => {
expectTypeOf(params).toEqualTypeOf<{ id: string }>()
return HttpResponse.text('non-matching')
return HttpResponse.text(
// @ts-expect-error Response body doesn't match the response type.
'non-matching',
)
}),
)
})
Expand Down
31 changes: 31 additions & 0 deletions test/typings/http.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ it('supports narrow object as a response body generic argument', () => {
})
})

it('supports object with extra keys as a response body generic argument', () => {
type ResponseBody = {
[key: string]: number | string
id: 123
}

http.get<never, never, ResponseBody>('/user', () => {
return HttpResponse.json({
id: 123,
// Extra keys are allowed if they satisfy the index signature.
name: 'John',
})
})

http.get<never, never, ResponseBody>('/user', () => {
return HttpResponse.json({
// @ts-expect-error Must be 123.
id: 456,
name: 'John',
})
})

http.get<never, never, ResponseBody>('/user', () => {
return HttpResponse.json({
id: 123,
// @ts-expect-error Must satisfy the index signature.
name: { a: 1 },
})
})
})

it('supports response body generic declared via type', () => {
type ResponseBodyType = { id: number }
http.get<never, never, ResponseBodyType>('/user', () => {
Expand Down

0 comments on commit 228c1b7

Please sign in to comment.