# TypeScriptの良さについて
----
## まずなんでこんなに流行ってんの?
----
## TypeScriptを使うメリット
----
1. 型安全
2. nullについて厳しい
3. タプル型がすごい
----
## 1. 型安全
----
### よくある例
数字の型 + 文字の型 = 文字?数字?
----
## 2. nullについて厳しい
----
### nullを許容した型の場合
```typescript=
type user = string | null
// userについてnullだったときのコードを書かないと怒られる。
if(!user) {
return console.error('userがnullです。')
}
```
----
## 3. タプル型がすごい
----
### [数字, 文字 * n]の型定義ができる
```typescript=
type NumAndStrings = [number, ...string[]];
const a1: NumAndStrings = [3, 'foo', 'bar'];
const a2: NumAndStrings = [5];
// エラー: Type 'string' is not assignable to type 'number'.
const a3: NumAndStrings = ['foo', 'bar'];
```
----
### [文字, 数字?]の型定義ができる
```typescript=
type T = [string, number?];
const t1: T = ['foo'];
const t2: T = ['foo', 3];
```
----
## 以上
----
今後ももっと流行っていきそうな流れあるので早めに触った方がいいかと思います。
{"metaMigratedAt":"2023-06-15T02:21:17.873Z","metaMigratedFrom":"YAML","title":"TypeScriptの良さについて","breaks":true,"contributors":"[{\"id\":\"3842a4fa-0ed2-4b74-b12e-d02562d24a05\",\"add\":976,\"del\":115}]"}