next.js: Infer Types leading to `props: never`
Describe the bug
export const getServerSideProps = async ({ params }) => {
return {
props: { foo: "bar" }
}
};
export const Page = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => { ... }
causes props: never
however the following works fine:
export const getServerSideProps = async ({ params }: GetServerSidePropsContext) => {
return {
props: { foo: "bar" }
}
};
export const Page = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => { ... }
as does:
export const getServerSideProps = async (ctx) => {
const params = ctx.params;
return {
props: { foo: "bar" }
}
};
export const Page = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => { ... }
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 26
- Comments: 28 (12 by maintainers)
It also happens in cases like this:
This also doesn’t work:
or
But removing the
GetServerSideProps
works:NextJS:
v10.0.1
For me, the magic was to make sure the
getServerSideProps
function args are explicitly typed using theGetServerSidePropsContext
type. Otherwise, the page props are inferred asany
type, e.g.user: any
.I’ve found a workaround - first of all - something broke with
11.1.x
as could useInferGetServerSidePropsType<typeof getServerSideProps>
even withnotFound
using the below.Here’s my hacky implementation that I just wrote:
utils/inferSSRProps.ts
pages/somePage.tsx
I solved the issue by publishing my own infer type:
notFound
redirect
getStaticProps
as well asgetServerSideProps
Install
Usage:
getStaticProps
getServerSideProps
https://www.npmjs.com/package/infer-next-props-type
Looks like this will hopefully be solved with Typescript 4.9!
https://twitter.com/leeerob/status/1563540593003106306
I found out something interessing. Type inference via
InferGetServerSidePropsType<typeof getServerSideProps>
seems to be working as long as I only return props.If i additionally return a redirect or a notFound conditionally type inference stops working for me.
This also works for me, but it would be more comfortable if it would work without the extra type specification. If the type is created like this, you would not need to infer it, because you can directly use it in the component.
returning an empty props in redirect worked for me:
@HaNdTriX any reason for not updating the built-in type?
Hey @KATT, thanks for your solution!
You do not need to cast
{ notFound: true }
toconst
if you change yourGetSSRResult
notFound
type toboolean
.utils/inferSSRProps.ts
Might be interesting https://github.com/microsoft/TypeScript/issues/38511
I think you’re right, however this one works, where the function isn’t typed, and nor is the argument:
I found a best work around. remove the
: GetStaticProps
fromconst getStaticProps = async () => {...}
Then the
const SomePage: NextPage<InferGetStaticPropsType<typeof getStaticProps>>= ({/* inferred types */}){}
can work correctly.Problem was as said here https://github.com/vercel/next.js/issues/32434#issuecomment-993013691. So all after adding the
: GetStaticProps
, the return type of getStaticProps would be extended to{ [key: string]: any; }
by TS because it includes the original type of {‘foo’: string}.Setting types like this works:
Because my type still has some edge cases to cover. Will deprecate the module as soon as we found the perfect working type and push the changes upstream.
This is a sneaky one, my string props were mistyped but that didn’t even cause a problem with strict TS. It only was an issue when I had an object in the props and wanted to access a property.
Here is my solution, key was to provide return type of getServerSideProps into GetServerSideProps type, here is example: https://gist.github.com/nenadfilipovic/f2dd9cb903da93a7d14ed1de6b3493b1