next.js + Typescript 手順メモ

imatomix
2020年5月15日 4:07
基本公式の通りにやる。

プロジェクト作成

  • プロジェクトを作成する
$ npx create-next-app
  • プロジェクトルートにtsconfig.jsonファイルを作成する。
$ touch tsconfig.json
  • この状態でnpm run dev すると、以下のようなメッセージがでる。
It looks like you're trying to use TypeScript but do not have the required package(s) installed. Please install typescript and @types/react by running: npm install --save-dev typescript @types/react If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files in your pages directory).
  • 言われた通りに typescript のパッケージをインストールする。
npm install --save-dev typescript @types/react
  • この状態で再びnpm run dev すると、プロジェクトルートにnext-env.d.tsが作成される。tsconfig.json にデフォルト設定が書き込まれる。
  • pages/ 以下の.jsファイルを.tsx に書き換える。
  • 再びnpm run dev するとアプリが起動する

その他