Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly infer parameter and return types in server.boundary() #2101

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}),
)