Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve PathParams type to support interface (#1219)
* feat: improve PathParams type to support interface * test: add * refactor: improve PathParams type * fix: remove optional from properties of PathParams type * chore: name generics, move path params test Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
- Loading branch information
1 parent
05e5a13
commit b70266f
Showing
5 changed files
with
105 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { rest } from 'msw' | ||
|
||
rest.get<never, { userId: string }>('/user/:userId', (req) => { | ||
req.params.userId | ||
|
||
// @ts-expect-error `unknown` is not defined in the request params type. | ||
req.params.unknown | ||
}) | ||
|
||
rest.get<never>('/user/:id', (req, res, ctx) => { | ||
const { userId } = req.params | ||
|
||
return res( | ||
ctx.body( | ||
// @ts-expect-error "userId" parameter is not annotated | ||
// and is ambiguous (string | string[]). | ||
userId, | ||
), | ||
) | ||
}) | ||
|
||
rest.get< | ||
never, | ||
// @ts-expect-error Path parameters are always strings. | ||
// Parse them to numbers in the resolver if necessary. | ||
{ id: number } | ||
>('/posts/:id', () => null) | ||
|
||
/** | ||
* Using interface as path parameters type. | ||
*/ | ||
interface UserParamsInterface { | ||
userId: string | ||
} | ||
|
||
rest.get<never, UserParamsInterface>('/user/:userId', (req) => { | ||
req.params.userId.toUpperCase() | ||
|
||
// @ts-expect-error Unknown path parameter "foo". | ||
req.params.foo | ||
}) | ||
|
||
/** | ||
* Using type as path parameters type. | ||
*/ | ||
type UserParamsType = { | ||
userId: string | ||
} | ||
|
||
rest.get<never, UserParamsType>('/user/:userId', (req) => { | ||
req.params.userId.toUpperCase() | ||
|
||
// @ts-expect-error Unknown path parameter "foo". | ||
req.params.foo | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters