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

TS getServerSideProps hinting disappears when using both type and infer #28543

Closed
driaug opened this issue Aug 26, 2021 · 3 comments
Closed

TS getServerSideProps hinting disappears when using both type and infer #28543

driaug opened this issue Aug 26, 2021 · 3 comments
Labels
bug Issue was opened via the bug report template. examples Issue was opened via the examples template.

Comments

@driaug
Copy link

driaug commented Aug 26, 2021

What example does this report relate to?

data-fetching (getServerSideProps with TS)

What version of Next.js are you using?

10.0.9

What version of Node.js are you using?

14.17.1

What browser are you using?

Chrome

What operating system are you using?

Windows (also happens on my mac)

How are you deploying your application?

next dev

Describe the Bug

When using both the GetServerSideProps type and InferGetServerSidePropsType, the type of the passed down props is any.

For example:

// We need this GetServerSideProps type when we want to have complete typing on the context
export const getServerSideProps: GetServerSideProps = async context => {
  const test = {
    value: 'test'
  }

  // The type of test here is {value: string}
  return {
    props: {
      test,
    },
  };
};

export default function Index({test}: InferGetServerSidePropsType<typeof getServerSideProps>) {
  // When trying to access our passed down prop here, the type is now any.
  return (
    <p>{test.value}</p> //
  );
}

image
(Do not mind the parts underlined in red, that is just ESLint complaining :))

Expected Behavior

You should be able to combine both GetServerSideProps type and InferGetServerSidePropsType without losing the typing.

The GetServerSideProps type, as far as I know, is essential for the typing of the context and TS will complain in strict setups if you remove it since it turns the context into an implicit any.

Removing the context enables you to also remove the GetServerSideProps type but if you need both the context-typing and the infered typing, I'm pretty sure there is no solution.

To Reproduce

export const getServerSideProps: GetServerSideProps = async context => {
  const test = {
    value: 'test'
  }

  // The type of test here is {value: string}
  return {
    props: {
      test,
    },
  };
};

export default function Index({test}: InferGetServerSidePropsType<typeof getServerSideProps>) {
  // When trying to access our passed down prop here, the type is now any.
  return (
    <p>{test.value}</p> //
  );
}

This happens both in the latest version of VS Code and Jetbrains Webstorm (the hinting and code-completion being any that is)

@driaug driaug added bug Issue was opened via the bug report template. examples Issue was opened via the examples template. labels Aug 26, 2021
@ijjk
Copy link
Member

ijjk commented Aug 26, 2021

Closing as a duplicate of #15913

@ijjk ijjk closed this as completed Aug 26, 2021
@driaug
Copy link
Author

driaug commented Aug 26, 2021

Oh my, terribly sorry! Completely missed that one :)

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. examples Issue was opened via the examples template.
Projects
None yet
Development

No branches or pull requests

3 participants