Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs: ts(7031) type error: Binding element 'Component' implicitly has an 'any' type

This is an issue I had when converting a NextJS project to TypeScript. In my _app.tsx, I got a type error: Binding element 'pageProps' implicitly has an 'any' type. ts(7031). The error likely looks like this: enter image description here

I know that there are existing answers for this somewhere on StackOverflow, but I am writing this so that someone in the future might come across this easier.

like image 367
Henrik VT Avatar asked May 08 '26 01:05

Henrik VT


1 Answers

Edit for 2024: This information is somewhat outdated. It does not apply to the App Directory.


The solution to this is simple. NextJS exports a custom type to resolve this issue: AppProps. It can be imported like this:

import { AppProps } from 'next/app';

To apply the type, you can reformat the props from

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

to

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

The final product should look like this, assuming an unmodified _app.tsx file:

import { AppProps } from 'next/app';

import '../styles/globals.css'

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

export default MyApp

like image 175
Henrik VT Avatar answered May 10 '26 18:05

Henrik VT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!