「Type ‘Props’ does not satisfy the constraint ‘PageProps’.」と表示された時の対処法

Next.jsでビルドした際に以下のエラーが表示されました。

Type error: Type 'Props' does not satisfy the constraint 'PageProps'.
  Types of property 'params' are incompatible.
    Type '{ id: string; }' is missing the following properties from type 'Promise<any>': then, catch, finally, [Symbol.toStringTag]

このエラーはPropsの値をPromise<any>の型に修正し、受け取り時に非同期にするとエラーが解決します。

修正前のコードは以下です。

type Props = {
    params: {
        id: string;
    };
}

修正後のコードは以下です。

type Props = {
    params: Promise<{
        id: string;
    }>;
}
async function Page({ params }) {
  const { id } = await params
  return <p>ID: {id}</p>
}

上記のエラーはビルド時に表示されるもので、ビルドせずに稼働中にページにアクセスした場合は、以下のような警告が表示されました。

hoge should be awaited before using its properties. Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis

参考:https://nextjs.org/docs/messages/sync-dynamic-apis

プログラミングの独学におすすめ
プログラミング言語の人気オンラインコース
独学でプログラミングを学習している方で、エラーなどが発生して効率よく勉強ができないと悩む方は多いはず。Udemyは、プロの講師が動画で実際のプログラムを動かしながら教えてくれるオンライン講座です。講座の価格は、セール期間中には専門書籍を1冊買うよりも安く済むことが多いです。新しく学びたいプログラミング言語がある方は、ぜひUdemyでオンライン講座を探してみてください。
目次