Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Fix definePage prop type inference bug #1

Open
NuroDev opened this issue Apr 3, 2023 · 0 comments
Open

Fix definePage prop type inference bug #1

NuroDev opened this issue Apr 3, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@NuroDev
Copy link
Owner

NuroDev commented Apr 3, 2023

What's the issue?

Currently when using definePage, either for the pages/ or app directory, if you add any argument to the handler, like to get the params from getStaticPaths or accessing the req for getServerSideProps, then the inferred props for Component break.

To break down what I have worked out so far:

  • The order you add additional properties to the options parameter can affect whether the type inference works (See example).
    • My guess is some kind of loop hierarchy bug where because getServerSideProps is before Component it knows to check for inferred props first, but when getServerSideProps is defined after Component it doesn't know getServerSideProps exists to fails over to the default of Record<string, any>?
  • Only happens when adding a parameter to a handler function, like { params } to getStaticProps.

TS Playground example

I also tried creating a discriminated union type to work around the issue but somehow it broke even more.
image

Example

✅ This one works perfectly fine

const { Component, getServerSideProps } = defineSSR({
	getServerSideProps: (ctx) => ({ props: { foo: "bar" } }),
	Component: (props) => <>Hello {props.foo}</>,
});

❌ This one does not & sets props to Record<string, any>

const { Component, getServerSideProps } = defineSSR({
	Component: (props) => <>Hello {props.foo}</>,
	getServerSideProps: (ctx) => ({ props: { foo: "bar" } }),
});
@NuroDev NuroDev added the bug Something isn't working label Apr 3, 2023
@NuroDev NuroDev self-assigned this Apr 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant