question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InferGetStaticPropsType always returns { [key: string]: any; }

See original GitHub issue

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

17.2

What browser are you using?

Chrome latest

What operating system are you using?

macOS

How are you deploying your application?

next dev

Describe the Bug

See below:

import { GetStaticProps, InferGetStaticPropsType, NextPage } from 'next'
import { pick } from 'contentlayer/client'

import { allArticles } from '.contentlayer/data'

const Home: NextPage = ({ posts }: InferGetStaticPropsType<typeof getStaticProps>) => {
  return (
    <div>hello world</div>
  )
}

export const getStaticProps: GetStaticProps = async () => {
  const posts = allArticles.map((post) =>
    pick(post, ['slug', 'title', 'summary', 'date'])
  )

  return { props: { posts: posts } }
}

export default Home

Expected Behavior

Inside the getStaticProps function, I am able to correctly use the type of posts

const posts: ConvertPick<{
    slug: string;
    title: string;
    summary: string;
    date: string;
}>[]

But inside of the main function, that doesn’t work.

To Reproduce

// tsconfig.json
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": ".",
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}
// package.json excerpt
"dependencies": {
    "contentlayer": "0.0.33",
    "next": "12.0.7",
    "next-contentlayer": "0.0.33",
    "next-images": "^1.8.4",
    "next-plausible": "^3.1.4",
    "next-themes": "0.0.15",
    "react": "17.0.2",
    "react-dom": "17.0.2",
  },
  "devDependencies": {
    "@types/node": "16.11.12",
    "@types/react": "17.0.37",
    "@typescript-eslint/eslint-plugin": "5.6.0",
    "@typescript-eslint/parser": "5.6.0",
    "eslint": "8.4.1",
    "eslint-config-next": "12.0.7",
    "eslint-config-prettier": "8.3.0",
    "eslint-plugin-mdx": "^1.16.0",
    "eslint-plugin-prettier": "4.0.0",
    "eslint-plugin-simple-import-sort": "7.0.0",
    "typescript": "4.5.3"
  }

Screen Shot 2021-12-12 at 18 37 45

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
balazsorban44commented, Dec 13, 2021

This is because you added GetStaticProps on line 12, without the generic passed to it. The default of prop for that type is { [key: string]: any; }.

See the docs: https://nextjs.org/docs/basic-features/data-fetching#typescript-use-getstaticprops

You want to let the return value be inferrable. If you need to type the params, you can use GetStaticPropsContext

0reactions
balazsorban44commented, Jan 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exploring [key:string]: any in TypeScript - DEV Community ‍ ‍
The first thing I did was to find out whether the interface was declared correctly, i.e. is it declaring that value contains an...
Read more >
next InferGetStaticPropsType TypeScript Examples
This page shows TypeScript code examples of next InferGetStaticPropsType. ... This includes setting the noindex header because static files always return a ...
Read more >
How to Build a Storefront with Next.js and BigCommerce
Storyblok always returns a story object {1} with some content {2}. In this case, the content has a ... [key: string]: React.ElementType,.
Read more >
getstaticprops not passing props - You.com | The AI Search ...
I have a [page].js where I form paths using getStaticPaths, fetch data from Firebase inside getStaticProps, and pass a props array to my...
Read more >
Data Fetching - Next.js
export async function getStaticProps(context) { return { props: {}, // will ... import { InferGetStaticPropsType } from 'next' type Post = { author:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found