TypeScript NextPage function getInitialProp static method with @typescript-eslint/unbound-method

Total
1
Shares

So I have the following code with typescript-eslint on with its recommended settings. I have read: Using getInitialProps in Next.js with TypeScript

const X: NextPage = props => {/*...*/}

X.getInitialProps = async (ctx): Promise<unknown> => {/*...*/}

X.getInitialProps emitted this error:

ESLint: Avoid referencing unbound methods which may cause unintentional scoping of this.(@typescript-eslint/unbound-method)

I believe this problem may be extended to, how to properly type static methods.


For those who aren’t familiar with Next.js, NextPage has the (simplified) definition of:

export type NextPage<C, IP = {}, P = {}> = ComponentType<P> & { // ComponentType is a React type
  getInitialProps?(context: C): IP
}

Solution

The GitHub repo of typescript-eslint has recently merged a pull request that fixes the issue https://github.com/typescript-eslint/typescript-eslint/pull/1736

Leave a Reply

Your email address will not be published. Required fields are marked *