Skip to content

Commit

Permalink
fix: properly infer parameter and return types in server.boundary() (
Browse files Browse the repository at this point in the history
…#2101)

Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
Andarist and kettanaito authored Mar 19, 2024
1 parent 21f44d7 commit 1370736
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export class SetupServerApi
this.handlersController = new AsyncHandlersController(handlers)
}

public boundary<Fn extends (...args: Array<any>) => unknown>(
callback: Fn,
): (...args: Parameters<Fn>) => ReturnType<Fn> {
return (...args: Parameters<Fn>): ReturnType<Fn> => {
public boundary<Args extends Array<any>, R>(
callback: (...args: Args) => R,
): (...args: Args) => R {
return (...args: Args): R => {
return store.run<any, any>(
{
initialHandlers: this.handlersController.currentHandlers(),
Expand Down
6 changes: 3 additions & 3 deletions src/node/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface SetupServer extends SetupServerCommon {
*
* @see {@link https://mswjs.io/docs/api/setup-server/boundary `server.boundary()` API reference}
*/
boundary<Fn extends (...args: Array<any>) => unknown>(
callback: Fn,
): (...args: Parameters<Fn>) => ReturnType<Fn>
boundary<Args extends Array<any>, R>(
callback: (...args: Args) => R,
): (...args: Args) => R
}
27 changes: 27 additions & 0 deletions test/typings/server.boundary.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { setupServer } from 'msw/node'
import { test } from 'vitest'

const fn = (_args: { a: number }): string => 'hello'

const server = setupServer()
const bound = server.boundary(fn)

bound({ a: 1 }).toUpperCase()

bound({
// @ts-expect-error Expected number, got string.
a: '1',
})

bound({ a: 1 })
// @ts-expect-error Unknown method ".fooBar()" on string.
.fooBar()

test(
'should work',
server.boundary(({ expect }) => {
expect(true).toBe(true)
// @ts-expect-error Property 'doesntExist' does not exist on type 'ExpectStatic'
expect.doesntExist
}),
)

0 comments on commit 1370736

Please sign in to comment.