【问题标题】:TypeScript NextPage function getInitialProp static method with @typescript-eslint/unbound-methodTypeScript NextPage 函数 getInitialProp 静态方法与 @typescript-eslint/unbound-method
【发布时间】:2020-06-13 14:08:33
【问题描述】:

所以我有以下带有 typescript-eslint 的代码及其推荐设置。我已阅读:Using getInitialProps in Next.js with TypeScript

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

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

X.getInitialProps 发出此错误:

ESLint:避免引用可能导致this 无意作用域的未绑定方法。(@typescript-eslint/unbound-method)

我相信这个问题可能会扩展到,如何正确键入静态方法。


对于不熟悉 Next.js 的人,NextPage 的(简化)定义为:

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

【问题讨论】:

  • 似乎是 ESLint 的一个已知问题:github.com/typescript-eslint/typescript-eslint/issues/1256。很确定这也描述了您的用例。这意味着您可能只需要为您的代码行禁止该规则?也就是说,我并没有对你的代码发表任何坚定的意见,但我认为它没有任何明显的问题。

标签: reactjs typescript next.js typescript-eslint


【解决方案1】:

typescript-eslint 的 GitHub 存储库最近合并了一个拉取请求以修复问题 https://github.com/typescript-eslint/typescript-eslint/pull/1736

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 2021-12-25
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2019-06-28
    • 2019-08-15
    相关资源
    最近更新 更多