Skip to content

Commit 1370736

Browse files
Andaristkettanaito
andauthoredMar 19, 2024··
fix: properly infer parameter and return types in server.boundary() (#2101)
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
1 parent 21f44d7 commit 1370736

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed
 

‎src/node/SetupServerApi.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export class SetupServerApi
6060
this.handlersController = new AsyncHandlersController(handlers)
6161
}
6262

63-
public boundary<Fn extends (...args: Array<any>) => unknown>(
64-
callback: Fn,
65-
): (...args: Parameters<Fn>) => ReturnType<Fn> {
66-
return (...args: Parameters<Fn>): ReturnType<Fn> => {
63+
public boundary<Args extends Array<any>, R>(
64+
callback: (...args: Args) => R,
65+
): (...args: Args) => R {
66+
return (...args: Args): R => {
6767
return store.run<any, any>(
6868
{
6969
initialHandlers: this.handlersController.currentHandlers(),

‎src/node/glossary.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface SetupServer extends SetupServerCommon {
7070
*
7171
* @see {@link https://mswjs.io/docs/api/setup-server/boundary `server.boundary()` API reference}
7272
*/
73-
boundary<Fn extends (...args: Array<any>) => unknown>(
74-
callback: Fn,
75-
): (...args: Parameters<Fn>) => ReturnType<Fn>
73+
boundary<Args extends Array<any>, R>(
74+
callback: (...args: Args) => R,
75+
): (...args: Args) => R
7676
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { setupServer } from 'msw/node'
2+
import { test } from 'vitest'
3+
4+
const fn = (_args: { a: number }): string => 'hello'
5+
6+
const server = setupServer()
7+
const bound = server.boundary(fn)
8+
9+
bound({ a: 1 }).toUpperCase()
10+
11+
bound({
12+
// @ts-expect-error Expected number, got string.
13+
a: '1',
14+
})
15+
16+
bound({ a: 1 })
17+
// @ts-expect-error Unknown method ".fooBar()" on string.
18+
.fooBar()
19+
20+
test(
21+
'should work',
22+
server.boundary(({ expect }) => {
23+
expect(true).toBe(true)
24+
// @ts-expect-error Property 'doesntExist' does not exist on type 'ExpectStatic'
25+
expect.doesntExist
26+
}),
27+
)

0 commit comments

Comments
 (0)
Please sign in to comment.