2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Next.js(create-next-app) 10.2.1からTypeScriptを利用したブートストラップがちょっとわかりやすくなる

Last updated at Posted at 2021-05-17

v10.2.1はまだ正式リリースされていません(2021/5/17現在)が、v10.2.1-canary.3からcreate-next-appコマンドに--tsオプションが追加されました。

npx -y create-next-app@10.2.1-canary.8 helloworld-ts --use-npm --ts 

試しに10.2.1-canary.8で--tsを利用してブートストラップしてみると

スクリーンショット 2021-05-17 14.30.35.png

ページコンポーネントが最初からtsx、APIはtsで生成され

package.json
  "devDependencies": {
    "@types/next": "9.0.0",
    "@types/react": "17.0.5",
    "error": "10.4.0",
    "typescript": "4.2.4"
  }

devDependenciesには型定義やtsが追加され

tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

tsconfig.jsonもちゃんと生成されます。

image.png

strictがちゃんとtrueなのも良いですね。もともとcreate-next-app -e with-typescriptexamplesから引っ張ってくる事でも似たような事は出来ていたんですが、一級市民としてのTSの地位がより強まったという感じでしょうか。

現状、create-next-app -e blog --tsのようにexamplesと組合せた使い方は出来ないので、そもそもデフォルトをtsとして--tsオプションではなく--without-tsオプションになってほしいというのがTypeScript好きとしては正直なところなのだけれど、そこまで行ったらNext.jsじゃなくてNext.tsになってしまうので無いだろうなまる

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?