Infer Types leading to `props: never`
See original GitHub issueDescribe 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>) => { ... }
Issue Analytics
- State:
- Created 3 years ago
- Reactions:26
- Comments:28 (11 by maintainers)
Top Results From Across the Web
How To Automatically Get Props Types in Next.js and TypeScript
js ≤ 12, both InferGetServerSidePropsType and InferGetStaticPropsType have a few major issues leading to Next.js infer never as props type.
Read more >Infer type from parent props - reactjs - Stack Overflow
I'd like to know if it's somehow possible, in TypeScript, to infer a type based on the prop ...
Read more >Hooks - React TypeScript Cheatsheets
Hooks are supported in @types/react from v16.8 up. ... See also the Using Inferred Types section if you need to use a complex...
Read more >How to type React props as a pro in TypeScript
There is one fix/workaround. We can add withRouter?:never to our WithStateProps type. Now it works and infers the type of {...props} ...
Read more >Documentation - Narrowing - TypeScript
Argument of type 'string | number' is not assignable to parameter of type ... has the advantage that TypeScript infers a narrow literal...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
.