副題は「スケールする JavaScript アプリケーション開発」。
p.11 の通りセットアップを行ったのだが、苦労した。
$ mkdir chapter04 $ cd chapter04 $ npm init -y Wrote to /mnt/c/Users/username/documents/program/typescript/chapter04/package.json: { "name": "chapter04", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } $ npm install --save-dev typescript tslint @types/node npm WARN deprecated tslint@6.1.3: TSLint has been deprecated in favor of ESLint. → Please see https://github.com/palantir/tslint/issues/4534 for more information. npm WARN chapter04@1.0.0 No description npm WARN chapter04@1.0.0 No repository field. npm ERR! code ENOENT npm ERR! syscall rename npm ERR! path /mnt/c/Users/usernamr/documents/program/typescript/chapter04/node_modules/typescript npm ERR! dest /mnt/c/Users/usernamr/documents/program/typescript/chapter04/node_modules/.typescript.DELETE npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, rename → '/mnt/c/Users/usernamr/documents/program/typescript/chapter04/node_modules/typescript' → -> '/mnt/c/Users/usernamr/documents/program/typescript/chapter04/node_modules/.typescript.DELETE' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! /home/usernamr/.npm/_logs/2021-03-05T01_37_31_375Z-debug.log $
うまくいっていないようだ。付録 H のようにやってみよう。
$ mkdir chapter04 $ cd chapter04 $ npm init -y (省略) $ npm i -D typescript npm WARN chapter04@1.0.0 No description npm WARN chapter04@1.0.0 No repository field. npm ERR! code ENOENT npm ERR! syscall rename npm ERR! path /mnt/c/Users/username/documents/program/typescript/chapter04/node_modules/typescript npm ERR! dest /mnt/c/Users/username/documents/program/typescript/chapter04/node_modules/.typescript.DELETE npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, → rename '/mnt/c/Users/username/documents/program/typescript/chapter04/node_modules/typescript' → -> '/mnt/c/Users/username/documents/program/typescript/chapter04/node_modules/.typescript.DELETE' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! /home/username/.npm/_logs/2021-03-05T01_45_30_136Z-debug.log
やはりうまくいかない。これはファイルの権限がないことで起きているエラーのようだ。sudo をつけて実行をやり直そう。
$ sudo npm i -D typescript added 1 package, and audited 2 packages in 14s found 0 vulnerabilities $ npx tsc --init message TS6071: Successfully created a tsconfig.json file. $ sudo npm i -D eslint@typescript-eslint/eslint-plugin @typescript-eslint/parser (後略)
うまくいったようだ。ただ、npm の前に sudo をしなければいけないのは仕方がないのだろうか。
p.88 の例を VS Code を使って打ってみた。なんか嫌な予感がして、それは現実になった。
type File = 'A' |'B' |'C' |'D' |'E' |'F' |'G' | 'H' type Rank = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
この File の下に赤い波線が現れ、次の問題が表示された。
識別子 'File' が重複しています。 ts(2300)
lib.dom.d.ts(5531, 11): ここでは 'File' も宣言されました。
lib.dom.d.ts(5536, 13): およびここで。
この File というのはチェスでいう A ~ H 軸(X軸)のことをいう。日本の将棋でいえば「筋」にあたる。 なお、Rank はチェスでいう 1 から 8 (Y軸)のことをいい、将棋では「段」に相当する。 こちらの表示はない。
けっきょくどうしたかというと、こちらの type File と type Rank の名前をそれぞれ type CFile と type CRank に変えた(C は Chess の意味)。正当な方法はモジュールを使うものだろうが、 よくわからない。
同じようなことが、p.92 から、Set の自前版を作るときにもおこった。こういうときは、 MySet などとするのだろうか。ただ、私の場合はせっかくなのでエスペラントで Aro という名前にしてみた。
紹介されているのがコードの断片なので、コード全体を通して適用すべき例が見当たらず、 わたしのような初学者には理解が難しかった。すでに大規模な JavaScript のコードを書いている人であれば、 役に立つのだろう。
誤植は見つからなかった。しかし、気になることがある。 副題の「スケールする JavaScript アプリケーション開発」にある「スケールする」 とはどういう意味であろうか。本書の原題は、 "Programming Typescript --- Making Your JavaScript Applicaions Scale" である。 意訳して、「大規模開発可能な JavaScript アプリケーション」ぐらいでどうだろうか。
書名 | プログラミング TypeScript |
著者 | Boris Cherny |
監訳者 | 今村 謙士 |
訳者 | 原 隆文 |
発行日 | 年 月 日 |
発行元 | オライリー・ジャパン |
定価 | 3400 円 |
サイズ | A4 変形判 |
ISBN | 978-4-87311-904-5 |
その他 | 越谷市立図書館で借りて読む |
まりんきょ学問所 > コンピュータについて > コンピュータの本 > JavaScript, altJS > Boris Cherny:プログラミング TypeScript