[TypeScript] 基礎文法2

### 配列
– 配列の作り方は[valu1, value2, …] と new Array()がある

const data = [10, 20, 30]
const total = data[0] + data[1] + data[2]
console.log('total:' + total)

– 変更不可の配列

let data2:readonly number[] = [10, 20, 30]
data2[0] = 100

Index signature in type ‘readonly number[]’ only permits reading.

– 配列の場合は for(let variables of value) を使う

const data = [100, 98, 76, 59, 87]
let total = 0
for (let item of data){
    total += item
}

const av = total / data.length
console.log('total:' + total)
console.log('average:' + av)

– 配列の要素の操作
L 最初に追加
data.unshift(value)
L 最初を削除
data.shift()
L 最後に追加
data.push(value)
L 最後を削除
data.pop()

let data:any = [10, 20, 30, 40, 50]

console.log(data)
for(let i = 0; i < 5; i++){
    data.pop()
    data.unshift('⭐️')
    console.log(data)
}

### タプル
変数の型を指定することで、異なる型も入れることができる

let me:[string, number]
let you:[string, number]

me = ['taro', 39]
you = ['hanako', 'hanako@gmail.com']

### enum
{}に用意した値しか設定できない。それ以外の値は使えない

enum janken {goo, choki, paa}

const you = janken.goo

switch(you) {
    case janken.goo:
    console.log('あいこ')
    break
    case janken.choki:
    console.log('win')
    break
    case janken.paa:
    console.log('lose')
    break    
}

### 型エイリアス
型に別名を入れることでタプルをわかりやすくする

type name = string
type age = number

let me:[name, age]
let you:[age, name]

me = ['taro', 39]
you = [29, 'hanako']

console.log(me)
console.log(you)

typeで型を指定

type name = string
type mail = string
type age = number
type person = [name, mail, age]

const taro:person = ['taro', 'taro@gmail.com', 39]
const hanako:person = ['hanako', 'hanako@gmail.com', 24]
const sachiko:person = ['sachiko', 'sachiko@gmail.com', 17]

const data:person[] = [taro, hanako, sachiko]

for(let item of data){
    console.log(item)
}

リテラル型
L リテラルとは直接ソースコードに書かれる値
L 指定した値しかない型

type hello = "hello"
type by = "bye"
type name = string

const taro:name = "taro"
const msg1:hello = "hello"
console.log(msg1 + ", " + taro)
const hanako:name = "hanako"
const msg2:by = "bye"
console.log(msg2 + ", " + hanako)

conditional types
L “|”を使うことで、enumと同じような使い方ができる

type msg = "hello" | "bye"
type name = string

const taro:name = "taro"
const msg1:msg = "hello"
console.log(msg1 + ", " + taro)
const hanako:name = "hanako"
const msg2:msg = "bye"
console.log(msg2 + ", " + hanako)

### 型チェック
– typeofで型チェックを行う

type id = number | string

const idA:id = "taro"
const idB:id = 123

const tp = idA

switch(typeof(tp)){
    case "number":
    console.log(tp + " is number")
    break
    case "string":
    console.log(tp + " is string")
    break
    default:
    console.log("type is undefined")
}

### ユーティリティ型
性質を付加する特殊な型
Readonly<●●>は総称型と呼ばれる機能

type data = [string, number]
type ReqData = Readonly<data>

const x:data = ["taro", 39]
const y:ReqData = ["hanako", 28]

x[1] = 28
y[1] = 17

Cannot assign to ‘1’ because it is a read-only property.

### シンボル
全ての値がユニークであることを保証される型
variable = Symbol(value)

const a1:string = "ok"
const b1:string = "ok"

console.log(a1 == b1)
console.log(a1 === b1)

const a2:unique symbol = Symbol("ok")
const b2:unique symbol = Symbol("ok")

console.log(a2 == b2)
console.log(a2 === b2)

This condition will always return ‘false’ since the types ‘typeof a2’ and ‘typeof b2’ have no overlap.
This condition will always return ‘false’ since the types ‘unique symbol’ and ‘string’ have no overlap.

### nullかも知れない値
値がないことを許容するために”?”をつける
絶対にnullではない場合、”!”をつける

type data = [name:string, age?:number]

const taro:data = ["taro", 39]
const hanako:data = ["hanako"]
console.log(taro)
console.log(hanako)

大分味が出てきた