Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Docs) getStaticProps & getServerSideProps TS section (#40607) #40639

Merged
merged 4 commits into from Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 39 additions & 4 deletions docs/api-reference/data-fetching/get-server-side-props.md
Expand Up @@ -104,18 +104,53 @@ export async function getServerSideProps(context) {

### getServerSideProps with TypeScript

For TypeScript, you can use the `GetServerSideProps` type from `next`:
The type of `getServerSideProps` can be specified using `GetServerSideProps` from `next`:

```ts
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (context) => {
// ...
type Data = { ... }

export const getServerSideProps: GetServerSideProps<{ data: Data }> = async (context) => {
const res = await fetch('https://.../data')
const data: Data = await res.json()

return {
props: {
data,
},
}
}
```

If you want to get inferred typings for your props, you can use `InferGetServerSidePropsType<typeof getServerSideProps>`:

```tsx
import { InferGetServerSidePropsType } from 'next'
import { GetServerSideProps } from 'next'

type Data = { ... }

export const getServerSideProps: GetServerSideProps<{ data: Data }> = async () => {
const res = await fetch('https://.../data')
const data: Data = await res.json()

return {
props: {
data,
},
}
}

function Page({ data }: InferGetServerSidePropsType<typeof getServerSideProps>) {
// will resolve data to type Data
}

export default Page
```

Implicit typing for `getServerSideProps` will also work properly:

```tsx
import { InferGetServerSidePropsType } from 'next'

Expand All @@ -133,7 +168,7 @@ export const getServerSideProps = async () => {
}

function Page({ data }: InferGetServerSidePropsType<typeof getServerSideProps>) {
// will resolve posts to type Data
// will resolve data to type Data
}

export default Page
Expand Down
49 changes: 46 additions & 3 deletions docs/api-reference/data-fetching/get-static-props.md
Expand Up @@ -202,18 +202,61 @@ export default Blog

## getStaticProps with TypeScript

You can use the `GetStaticProps` type from `next` to type the function:
The type of `getStaticProps` can be specified using `GetStaticProps` from `next`:

```ts
import { GetStaticProps } from 'next'

export const getStaticProps: GetStaticProps = async (context) => {
// ...
type Post = {
author: string
content: string
}

export const getStaticProps: GetStaticProps<{ posts: Post[] }> = async (
context
) => {
const res = await fetch('https://.../posts')
const posts: Post[] = await res.json()

return {
props: {
posts,
},
}
}
```

If you want to get inferred typings for your props, you can use `InferGetStaticPropsType<typeof getStaticProps>`:

```tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will tsx work? I am unsure (worth checking)

import { InferGetStaticPropsType } from 'next'
import { GetStaticProps } from 'next'

type Post = {
author: string
content: string
}

export const getStaticProps: GetStaticProps<{ posts: Post[] }> = async () => {
const res = await fetch('https://.../posts')
const posts: Post[] = await res.json()

return {
props: {
posts,
},
}
}

function Blog({ posts }: InferGetStaticPropsType<typeof getStaticProps>) {
// will resolve posts to type Post[]
}

export default Blog
```

Implicit typing for `getStaticProps` will also work properly:

```tsx
import { InferGetStaticPropsType } from 'next'

Expand Down