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

fix: InferGetServerSidePropsType and InferGetStaticPropsType #40635

Merged
merged 5 commits into from
Sep 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"eslint-plugin-react-hooks": "4.5.0",
"event-stream": "4.0.1",
"execa": "2.0.3",
"expect-type": "0.14.2",
"express": "4.17.0",
"faker": "5.5.3",
"faunadb": "2.6.1",
Expand Down
22 changes: 6 additions & 16 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,10 @@ export type GetStaticProps<
context: GetStaticPropsContext<Q, D>
) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>

export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
? P
: T extends (
context?: GetStaticPropsContext<any>
) => Promise<GetStaticPropsResult<infer P>> | GetStaticPropsResult<infer P>
? P
: never
export type InferGetStaticPropsType<T extends (args: any) => any> = Extract<
Awaited<ReturnType<T>>,
{ props: any }
>['props']

export type GetStaticPathsContext = {
locales?: string[]
Expand Down Expand Up @@ -181,16 +178,9 @@ export type GetServerSideProps<
context: GetServerSidePropsContext<Q, D>
) => Promise<GetServerSidePropsResult<P>>

export type InferGetServerSidePropsType<T> = T extends GetServerSideProps<
infer P,
any
export type InferGetServerSidePropsType<T extends (args: any) => any> = Awaited<
Extract<Awaited<ReturnType<T>>, { props: any }>['props']
>
? P
: T extends (
context?: GetServerSidePropsContext<any>
) => Promise<GetServerSidePropsResult<infer P>>
? P
: never

declare global {
interface Crypto {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions test/unit/infer-get-server-side-props-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type {
InferGetServerSidePropsType,
GetServerSidePropsContext,
} from 'next'
import { expectTypeOf } from 'expect-type'

describe('InferGetServerSidePropsType', () => {
it('should work with sync functions', async () => {
function getServerSideProps(context: GetServerSidePropsContext) {
if (context.params?.notFound) {
return {
notFound: true,
}
}

return {
props: {
foo: 'bar',
},
}
}

type PageProps = InferGetServerSidePropsType<typeof getServerSideProps>

expectTypeOf<PageProps>().toEqualTypeOf<{ foo: string }>()
})

it('should work with async functions', async () => {
async function getServerSideProps(context: GetServerSidePropsContext) {
if (context.params?.notFound) {
return {
notFound: true,
}
}

if (context.params?.redirect) {
return {
redirect: {
destination: '/',
},
}
}

return {
props: {
foo: 'bar',
},
}
}

type PageProps = InferGetServerSidePropsType<typeof getServerSideProps>

expectTypeOf<PageProps>().toEqualTypeOf<{ foo: string }>()
})

it('should work with promised props', async () => {
async function getServerSideProps() {
return {
props: Promise.resolve({
foo: 'bar',
}),
}
}

type PageProps = InferGetServerSidePropsType<typeof getServerSideProps>

expectTypeOf<PageProps>().toEqualTypeOf<{ foo: string }>()
})
})
52 changes: 52 additions & 0 deletions test/unit/infer-get-static-props.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { InferGetStaticPropsType, GetStaticPropsContext } from 'next'
import { expectTypeOf } from 'expect-type'

describe('InferGetServerSidePropsType', () => {
it('should work with sync functions', async () => {
function getStaticProps(context: GetStaticPropsContext) {
if (context.params?.notFound) {
return {
notFound: true,
}
}

return {
props: {
foo: 'bar',
},
}
}

type PageProps = InferGetStaticPropsType<typeof getStaticProps>

expectTypeOf<PageProps>().toEqualTypeOf<{ foo: string }>()
})

it('should work with async functions', async () => {
async function getStaticProps(context: GetStaticPropsContext) {
if (context.params?.notFound) {
return {
notFound: true,
}
}

if (context.params?.redirect) {
return {
redirect: {
destination: '/',
},
}
}

return {
props: {
foo: 'bar',
},
}
}

type PageProps = InferGetStaticPropsType<typeof getStaticProps>

expectTypeOf<PageProps>().toEqualTypeOf<{ foo: string }>()
})
})