小编kek*_*eke的帖子

使用返回类型为 NextPage 的函数时出现类型错误

所以下面的代码有效:

const HomePage: NextPage = () => (
  <div>
    <div>HomePage</div>
  </div>
);
Run Code Online (Sandbox Code Playgroud)

不过,我想遵守 Airbnb 的风格指南,在本例中,它要求您使用命名函数,得出以下代码:

function HomePage(): NextPage {
  return (
    <div>
      <div>HomePage</div>
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)

然而,编译器对上面的代码给出了以下错误:

Type 'Element' is not assignable to type 'NextPage<{}, {}>'.
  Type 'ReactElement<any, any>' is not assignable to type 'FunctionComponent<{}> & { getInitialProps?(context: NextPageContext): {} | Promise<{}>; }'.
    Type 'ReactElement<any, any>' is not assignable to type 'FunctionComponent<{}>'.
      Type 'ReactElement<any, any>' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any, any> …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs next.js

4
推荐指数
1
解决办法
2016
查看次数

标签 统计

next.js ×1

reactjs ×1

typescript ×1