Skip to content

Commit

Permalink
Update gSSP type to support props as a promise (#28999)
Browse files Browse the repository at this point in the history
In a previous PR, `getServerSideProps` was altered to support returning
`props` as a promise. This change updates the TS types to permit promises
as well, so you can write type `GetServerSideProps<Props>` instead of
`GetServerSideProps<Promise<Props>>`. e.g.:

```typescript
type Props = {
  data: string
}

export const getServerSideProps: GetServerSideProps<Props> = async (
  context
) => {
  return {
    props: (async function () {
      return { data: 'some data' }
    })(),
  }
}
```

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
kara authored Sep 10, 2021
1 parent e09b7a5 commit c27e3a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export type GetServerSidePropsContext<
}

export type GetServerSidePropsResult<P> =
| { props: P }
| { props: P | Promise<P> }
| { redirect: Redirect }
| { notFound: true }

Expand Down
19 changes: 19 additions & 0 deletions test/integration/typescript/pages/ssr/promise.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GetServerSideProps } from 'next'

type Props = {
data: string
}

export const getServerSideProps: GetServerSideProps<Props> = async (
context
) => {
return {
props: (async function () {
return { data: 'some data' }
})(),
}
}

export default function Page({ data }: Props) {
return <h1> {data} </h1>
}

0 comments on commit c27e3a4

Please sign in to comment.