How to strongly type fetch with Typescript
I would like to create a custom fetch function which thanks to TypeScript has strongly type params and returns. Imagine I declare types like this interface ResponseData { status: 'success' | 'error'; } interface RequestBody { foo: string; bar: number; } const endpoint = { url: '/endpointurl/', method: 'get', /* A way to associate RequestBody and ResponseData types */ }; I would like to call my custom fetch function with endpoint object and automatically have typing on the body I pass in params and the data I get in returns /* myfetch would use endpoint.url and endpoint.method to fetch data */
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.