LoginSignup
0
1

More than 3 years have passed since last update.

ポケモンでTypeScriptおもしれーってなる(型エイリアスまで)

Last updated at Posted at 2020-11-25

型エイリアス typeとは

型宣言をtypeで記述することができる。

type Age = number
let age: Age = 15

オブジェクト

もちろんオブジェクトもできる。
?を使うと必ず記述しなくても良くなる。

let o: { a: number, b?: string } = {
  a: 2
}
console.log(o.a) // -> 2
console.log(o.b) // -> undefined

readonly

オブジェクトのプロパティを書き換え禁止にできる。
変数で言うconst。

let o3: {
  readonly id: number
} = { id: 3 }
o3.id = 2 // -> エラー

ポケモンとtypeでいちゃいちゃする

以上を踏まえてあれこれ触る。

type Action = {1: string, 2?: string, 3?: string, 4?: string }
type Pokemon = {
  readonly abst: string,
  readonly name: string,
  actions: Action
}
let pityu: Pokemon = {
  abst: 'ピカチュウの進化前',
  name: 'ピチュー',
  actions: {1: 'たいあたり', 2: 'しっぽをふる'}
}
pikatyu.actions[3] = "でんきショック"
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1