### コンパイラ導入
$ node -v
v10.19.0
$ sudo npm install -g typescript
$ tsc -v
Version 4.4.3
hello.ts
const message:string = 'Hello! TypeScript!' console.log(message);
$ tsc hello.ts
$ ls
hello.js hello.ts
$ node hello.js
Hello! TypeScript!
### npm環境
$ npm install typescript ts-loader webpack webpack-cli webpack-dev-server –save-dev
package.json
// 省略 "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack --mode=development", "start": "webpack-dev-server --mode=development" }, // 省略
webpack.config.js
const path = require('path'); module.exports = { entry: { bundle: './src/app.ts' }, output: { path: path.join(__dirname,'dist'), filename: '[name].js' }, resolve: { extensions:['.ts','.js'] }, devServer: { contentBase: path.join(__dirname,'dist') }, module: { rules: [ { test:/\.ts$/,loader:'ts-loader' } ] } }
$ tsc –init
-> tsconfig.json が生成される
dist, srcフォルダを作成する
src/app.ts
import {Item} from './item'; var elem = document.getElementById('output'); var aBook = new Item('はじめてのTypeScript', 2020); aBook.say(elem);
src/item.ts
export class Item { constructor(private name:string, private price:number){} public say(elem: HTMLElement | null) : void { if(elem){ elem.innerHTML = '書名:' + this.name + ' 価格: ' + this.price + '円'; } } }
dist/index.html
<body> <div id="output"></div> <script src="bundle.js"></script> </body> </html>
$ npm run start
> tutorial@1.0.0 start /home/vagrant/dev/typescript/tutorial
> webpack-dev-server –mode=development
[webpack-cli] /home/vagrant/dev/typescript/tutorial/node_modules/webpack-dev-server/lib/servers/WebsocketServer.js:10
static heartbeatInterval = 1000;
何でだろう? なんかエラーになるな。。
$ sudo apt-get purge nodejs
▼この記事を参考に最新版を入れる
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04-ja
$ cd ~
$ curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
$ sudo bash nodesource_setup.sh
$ sudo apt install nodejs
$ node -v
v14.17.6
$ npm run start
http://192.168.34.10:8000/
nodeがv10だとwebpack-dev-serverでエラーになるみたい
https://github.com/VickScarlet/lifeRestart/issues/176
v14にupgradeするとエラー解消
マジかよ~~~~ もうやだー