Vue.jsとNuxt.jsでいろいろ作っているけれど、TypeScriptが素敵すぎる。。
create-nuxt-app
だとTypeScriptがないので、TypeScript化するときにやることを整理したときのメモ
TypeScriptが使えるように設定する
0. ディレクトリ構成
ディレクトリ構成はこんな感じ。
- プロジェクトはcreate-nuxt-appで作成
- Firebase Functionsも使うので、app配下をsrcDirに変更している
. ├── app ... NuxtのsrcDir │ ├── assets │ ├── components │ ├── layouts │ ├── middleware │ ├── pages │ ├── plugins │ ├── static │ └── store ├── functions ... Firebase FunctionのsrcDir │ ├── src │ ├── package.json │ └── tsconfig.json ├── nuxt.config.js └── package.json
1. npmでパッケージのインストール
$ npm install --save nuxt-property-decorator vuex-class $ npm install --save -D @types/node ts-loader typescript
2. index.d.tsの追加
TypeScriptで.vue
ファイルを利用できるようにapp/index.d.ts
を追加する
declare module "*.vue" { import Vue from "vue"; const _default: Vue; export default _default; }
3. typesを配置するディレクトリを用意
nuxt-community/typescript-templateを参考に、
app/types
ディレクトリと、以下のファイルを作成
app/types/index.ts
... 空ファイルapp/types/state.ts
export * from "./state";
4. modules/typescript.js
を追加
nuxt-community/typescript-templateのものをコピーしてくる。
export default function() { // Add .ts & .tsx extension to Nuxt this.nuxt.options.extensions.push("ts", "tsx"); // Extend webpack build this.extendBuild(config => { // Add TypeScript config.module.rules.push({ test: /\.tsx?$/, loader: "ts-loader", options: { appendTsSuffixTo: [/\.vue$/] } }); // Add .ts extension in webpack resolve if (!config.resolve.extensions.includes(".ts")) { config.resolve.extensions.push(".ts"); } // Add .tsx extension in webpack resolve if (!config.resolve.extensions.includes(".tsx")) { config.resolve.extensions.push(".tsx"); } }); }
追加したモジュールをnuxt.config.js
に追加
module.exports = { modules: [ "~/modules/typescript.js" ] }
5. tsconfig.json
を追加
これもnuxt-community/typescript-templateのものをコピーしてくる。
{ "compilerOptions": { "target": "es5", "lib": ["dom", "es2015"], "module": "es2015", "moduleResolution": "node", "experimentalDecorators": true, "noImplicitAny": false, "noImplicitThis": false, "strictNullChecks": true, "removeComments": true, "suppressImplicitAnyIndexErrors": true, "allowSyntheticDefaultImports": true, "allowJs": false, "baseUrl": ".", "paths": { "~/*": ["./app/*"] } } }
6. 設定後のディレクトリ構成
設定はこれで完了。変更後のディレクトリ構成はこんな感じ。
. ├── app ... NuxtのsrcDir │ ├── assets │ ├── components │ ├── layouts │ ├── middleware │ ├── modules │ │ └── ★typescript.js │ ├── pages │ ├── plugins │ ├── static │ ├── store │ ├── ★types │ │ ├── ★index.ts │ │ └── ★state.ts │ └── ★index.d.ts ├── functions ... Firebase FunctionのsrcDir │ ├── src │ ├── package.json │ └── tsconfig.json ├── ★nuxt.config.js ├── package.json └── ★tsconfig.json
.vueファイルの内容をTypeScriptに変更する
あとは、各.vue
ファイルを変更していく。
変更前
<script> export default { head: function() { return { title: "タイトル" } }, props: { name: { type: string, default: "なまえ" } }, data(): { return { flag: false }; }, computed: { isEmptyName: function() { return this.name === ""; } }, methods: { toggleFlag: function() { this.flag = !this.flag; } } } </script>
変更後
Component
は、@Component
に設定するdata/props
は、fieldとして定義。props
は@Prop()
が必要computed
は、get
アクセサとして定義method
やライフサイクルの関数は、methodとして設定する- 必要に応じてアノテーションを付ける。
@Watch
や@Emit
など
- 必要に応じてアノテーションを付ける。
<script lang="ts"> import { Component, Vue } from "nuxt-property-decorator"; @Component({}) // ※必須. コンポーネントを使ってなくても必要 export default class extends Vue { /** head() */ private head() { return { title: "タイトル" }); } /** prop() */ @Prop() name: string = "なまえ"; /** data() */ private flag: boolean = false; /** computed() */ public get isEmptyName(): boolean { return this.name === ""; } /** methods() */ private toggleFlag(): void { this.flag = !this.flag; } } </script>
以上!! 型のある開発はやっぱり素敵
参考になる書籍

JavaScriptエンジニアがTypeScriptの特徴を「超」手っ取り早く大まかに把握するための本
- 作者: 天田士郎
- 発売日: 2018/03/09
- メディア: Kindle版
- この商品を含むブログを見る

TypeScript実践プログラミング (Programmer's SELECTION)
- 作者: スティーブ・フェントン,鈴木幸敏,株式会社クイープ
- 出版社/メーカー: 翔泳社
- 発売日: 2015/01/23
- メディア: 大型本
- この商品を含むブログ (1件) を見る

Nuxt.jsビギナーズガイド―Vue.js ベースのフレームワークによるシングルページアプリケーション開発
- 作者: 花谷拓磨
- 出版社/メーカー: シーアンドアール研究所
- 発売日: 2018/10/17
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る

- 作者: 川口和也,喜多啓介,野田陽平,手島拓也,片山真也
- 出版社/メーカー: 技術評論社
- 発売日: 2018/09/22
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る
参考にしたサイト様
- TypeScript のサポート — Vue.js
- TypeScriptでNuxtアプリケーションを作成する際の覚書 | Studio Andy
- Nuxt.jsでTypeScriptを使うために色々試して諦めた - memo_md
- TypeScriptでNuxtアプリを作るチュートリアル【書籍検索システム】 | WINDII
- Nuxt + Typescriptセットアップ - Qiita
- Nuxt2 + TypeScriptの環境構築 - 備忘録
- GitHub - nuxt-community/typescript-template: Typescript starter with Nuxt.js
- FN1609006 | TypeScript入門 05: get/setアクセサをを使う | HTML5 : テクニカルノート
- vue.js + typescript = vue.ts ことはじめ - Qiita
- Nuxt.js+TypeScriptでGitHub Viewerを作ってみた
- 次にJSガッツリ書くときはTypeScriptで - Qiita