|
| 1 | +# Inferring Types |
| 2 | + |
| 3 | +Often, we need to manually extract the request or responses types of specific contract endpoints, so functions, services, lambdas, React components, etc. can be safely typed |
| 4 | +when there is no automatic type inference. |
| 5 | + |
| 6 | +We have separate type helpers for server-side and client-side code since we need to infer either the Input or Output Zod types depending on |
| 7 | +where the code is used. |
| 8 | + |
| 9 | +## Inferring Response Types |
| 10 | + |
| 11 | +To get the response types of a contract or a specific endpoint, we have the following type helpers: |
| 12 | + |
| 13 | +- `ServerInferResponses<AppRouter | AppRoute, OptionalHttpStatusCode>` |
| 14 | +- `ClientInferResponses<AppRouter | AppRoute, OptionalHttpStatusCode>` |
| 15 | + |
| 16 | +```typescript |
| 17 | +import { ServerInferResponses } from '@ts-rest/core'; |
| 18 | +import { contract } from './contract'; |
| 19 | + |
| 20 | +type ResponseShapes = ServerInferResponses<typeof contract>; |
| 21 | + |
| 22 | +async function someHttpCall(req: Request): Promise<ResponseShapes['getPosts']> { |
| 23 | + return ...; |
| 24 | +} |
| 25 | + |
| 26 | +function someServiceCall(): ServerInferResponses<typeof contract.getPosts> { |
| 27 | + return ...; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### Inferring Response Body |
| 32 | + |
| 33 | +If you need to infer the response body for a defined response status of a specific endpoint, we can use the following type helpers: |
| 34 | + |
| 35 | +- `ServerInferResponseBody<AppRoute, OptionalHttpStatusCode>` |
| 36 | +- `ClientInferResponseBody<AppRoute, OptionalHttpStatusCode>` |
| 37 | + |
| 38 | +This is syntactic sugar for `ServerInferResponses<AppRoute, OptionalHttpStatusCode & keyof AppRoute['responses']>['body']` |
| 39 | + |
| 40 | +```typescript |
| 41 | +import React from 'react'; |
| 42 | +import { ClientInferResponseBody } from '@ts-rest/core'; |
| 43 | +import { contract } from './contract'; |
| 44 | + |
| 45 | +type Post = ClientInferResponseBody<typeof contract.getPost, 200>; |
| 46 | + |
| 47 | +function PostComponent(props: { post: Post }) { |
| 48 | + return <>...</>; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Inferring Request Types |
| 53 | + |
| 54 | +To get the request (path params, query params, body) types of a contract or a specific endpoint, we have the following type helpers: |
| 55 | + |
| 56 | +- `ServerInferRequest<AppRouter | AppRoute>` |
| 57 | +- `ClientInferRequest<AppRouter | AppRoute>` |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { ServerInferRequest, ServerInferResponses } from '@ts-rest/core'; |
| 61 | +import { contract } from './contract'; |
| 62 | + |
| 63 | +type GetPostRequest = ServerInferRequest<typeof contract.getPost>; |
| 64 | +type GetPostResponse = ServerInferResponses<typeof contract.getPost>; |
| 65 | + |
| 66 | +async function getPostLambdaHandler({ params, query }: GetPostRequest): Promise<GetPostResponse> { |
| 67 | + return ...; |
| 68 | +} |
| 69 | +``` |
0 commit comments