www.oreilly.com
Introduction
- 型安全
- using types to prevent programs from doing invalid things.
- invalidってなに
- 実行する前に、テストする前にわかるのが強い
The Compiler
- プログラミング言語の一般論
- コンパイラの仕事
- ソースコードファイル -> AST: Abstract Syntax Tree
- AST -> バイトコード
- 【所感】ASTを作るのはレキサとパーサな気がするぞ
- バイトコードをランタイムに入力して評価させることで所望の結果を得る
- TS独特なところ
- バイトコードではなくJSコードを出力する
- AST生成後、JSコード出力前に型検査を行う
- TSのAST -> JSコードの変換時には型情報は使われない
The Type System
- 型システムは2つに大別される
- これらはトレードオフ
- TSは両方にインスパイアされている
- 型の明示は最小限にしたほうがいい
How are types bound?
- TS: gradually typed language (漸進的型付け)
- 全てが静的に型付けされていなくてもコンパイルが通る
- 型が与えられていない部分では間違いを見逃すことがある
- レガシーなJSコードからTSに漸進的に移行するときに有用
- ただし、そうでないならば型カバレッジ100%を目指すべき
Are types automatically converted?
When are types checked?
when are errors surfaced?
- コンパイル時エラー
- 実行時エラー
- スタックオーバーフロー
- ネットワークコネクションエラー
- ユーザー入力不正
- 全部実行時エラーなpure JSより良い
Code Editor Setup
npm init -y
npm install --save-dev typescript tslint @types/node
tsconfig.json
npx tsc --init
{
"compilerOptions": {
"lib": ["es2015"],
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"strict": true,
"target": "es2015"
},
"include": [
"src"
]
}
npx tslint --init
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"rules": {
"semicolon": [true, "never"],
"trailing-comma": [true, "always"]
}
}
npx tslint -c tslint.json './src/**/*.ts'
index.ts
console.log('Hello TypeScript!')
npx tsc
node ./dist/index.js
Hello TypeScript!
shortcuts
ts-node
でTSを直接実行
typescript-node-starter
でスキャフォールディング
英語