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: use satisfies for TypeScript code blocks #55205

Merged
merged 4 commits into from
Sep 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,21 @@ For [`getStaticProps`](/docs/pages/api-reference/functions/get-static-props), [`
```tsx filename="pages/blog/[slug].tsx"
import { GetStaticProps, GetStaticPaths, GetServerSideProps } from 'next'

export const getStaticProps: GetStaticProps = async (context) => {
export const getStaticProps = (async (context) => {
// ...
}
}) satisfies GetStaticProps

export const getStaticPaths: GetStaticPaths = async () => {
export const getStaticPaths = (async () => {
// ...
}
}) satisfies GetStaticPaths

export const getServerSideProps: GetServerSideProps = async (context) => {
export const getServerSideProps = (async (context) => {
// ...
}
}) satisfies GetServerSideProps
```

> **Good to know:** `satisfies` was added to TypeScript in [4.9](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html). We recommend upgrading to the latest version of TypeScript.

## API Routes

The following is an example of how to use the built-in types for API routes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Repo = {
stargazers_count: number
}

export const getStaticProps: GetStaticProps<{
repo: Repo
}> = async () => {
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
}) satisfies GetStaticProps<{
repo: Repo
}>

export default function Page({
repo,
Expand All @@ -29,7 +29,7 @@ export default function Page({
```

```jsx filename="pages/index.js" switcher
export const getStaticProps = async () => {
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Repo = {
stargazers_count: number
}

export const getStaticPaths: GetStaticPaths = async () => {
export const getStaticPaths = (async () => {
return {
paths: [
{
Expand All @@ -30,15 +30,15 @@ export const getStaticPaths: GetStaticPaths = async () => {
],
fallback: true, // false or "blocking"
}
}
}) satisfies getStaticPaths
leerob marked this conversation as resolved.
Show resolved Hide resolved

export const getStaticProps: GetStaticProps<{
repo: Repo
}> = async () => {
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
}) satisfies GetStaticProps<{
repo: Repo
}>

export default function Page({
repo,
Expand All @@ -48,7 +48,7 @@ export default function Page({
```

```jsx filename="pages/repo/[name].js" switcher
export const getStaticPaths = async () => {
export async function getStaticPaths() {
return {
paths: [
{
Expand All @@ -61,7 +61,7 @@ export const getStaticPaths = async () => {
}
}

export const getStaticProps = async () => {
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Repo = {
stargazers_count: number
}

export const getServerSideProps: GetServerSideProps<{
repo: Repo
}> = async () => {
export const getServerSideProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
}) satisfies GetServerSideProps<{
repo: Repo
}>

export default function Page({
repo,
Expand All @@ -29,7 +29,7 @@ export default function Page({
```

```jsx filename="pages/index.js" switcher
export const getServerSideProps = async () => {
export async function getServerSideProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
Expand Down Expand Up @@ -99,10 +99,6 @@ This approach works well for user dashboard pages, for example. Because a dashbo
The following example shows how to fetch data at request time and pre-render the result.

```jsx
function Page({ data }) {
// Render data...
}

// This gets called on every request
export async function getServerSideProps() {
// Fetch data from external API
Expand All @@ -113,7 +109,9 @@ export async function getServerSideProps() {
return { props: { data } }
}

export default Page
export default function Page({ data }) {
// Render data...
}
```

## Caching with Server-Side Rendering (SSR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Repo = {
stargazers_count: number
}

export const getServerSideProps: GetServerSideProps<{
repo: Repo
}> = async () => {
export const getServerSideProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
}) satisfies GetServerSideProps<{
repo: Repo
}>

export default function Page({
repo,
Expand All @@ -29,7 +29,7 @@ export default function Page({
```

```jsx filename="pages/index.js" switcher
export const getServerSideProps = async () => {
export async function getServerSideProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Repo = {
stargazers_count: number
}

export const getStaticPaths: GetStaticPaths = async () => {
export const getStaticPaths = (async () => {
return {
paths: [
{
Expand All @@ -28,15 +28,15 @@ export const getStaticPaths: GetStaticPaths = async () => {
],
fallback: true, // false or "blocking"
}
}
}) satisfies getStaticPaths

export const getStaticProps: GetStaticProps<{
repo: Repo
}> = async () => {
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
}) satisfies GetStaticProps<{
repo: Repo
}>

export default function Page({
repo,
Expand All @@ -46,7 +46,7 @@ export default function Page({
```

```jsx filename="pages/repo/[name].js" switcher
export const getStaticPaths = async () => {
export async function getStaticPaths() {
return {
paths: [
{
Expand All @@ -59,7 +59,7 @@ export const getStaticPaths = async () => {
}
}

export const getStaticProps = async () => {
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Repo = {
stargazers_count: number
}

export const getStaticProps: GetStaticProps<{
repo: Repo
}> = async () => {
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
}) satisfies GetStaticProps<{
repo: Repo
}>

export default function Page({
repo,
Expand All @@ -29,7 +29,7 @@ export default function Page({
```

```jsx filename="pages/index.js" switcher
export const getStaticProps = async () => {
export async function getStaticPaths() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
Expand Down