Skip to content

Commit 3b7b776

Browse files
authored
fix(types): support optional path parameters (#2368)
1 parent 8ddb4ab commit 3b7b776

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/core/utils/matching/matchRequestUrl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { normalizePath } from './normalizePath'
44

55
export type Path = string | RegExp
66
export type PathParams<KeyType extends keyof any = string> = {
7-
[ParamName in KeyType]: string | ReadonlyArray<string>
7+
[ParamName in KeyType]?: string | ReadonlyArray<string>
88
}
99

1010
export interface Match {

test/typings/http.test-d.ts

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ it('supports a single path parameter', () => {
77
})
88
})
99

10+
it('supports a repeating path parameter', () => {
11+
http.get<{ id?: string }>('/user/id*', ({ params }) => {
12+
expectTypeOf(params).toEqualTypeOf<{ id?: string }>()
13+
})
14+
})
15+
16+
it('supports an optional path parameter', () => {
17+
http.get<{ id?: string }>('/user/:id?', ({ params }) => {
18+
expectTypeOf(params).toEqualTypeOf<{ id?: string }>()
19+
})
20+
})
21+
22+
it('supports optional repeating path parameter', () => {
23+
/**
24+
* @note This is the newest "path-to-regexp" syntax.
25+
* MSW doesn't support this quite yet.
26+
*/
27+
http.get<{ path?: string[] }>('/user{/*path}', ({ params }) => {
28+
expectTypeOf(params).toEqualTypeOf<{ path?: string[] }>()
29+
})
30+
})
31+
1032
it('supports multiple path parameters', () => {
1133
type Params = { a: string; b: string[] }
1234
http.get<Params>('/user/:a/:b/:b', ({ params }) => {

0 commit comments

Comments
 (0)