30
25

More than 5 years have passed since last update.

TypeScriptで連想配列を定義する

Posted at

TypeScriptで連想配列を書く時に迷ったのでメモ。

デモ


// 連想配列
let list1: { str: string; flag: boolean } = {
    str: 'string',
    flag: true
};

// 配列内連想配列
let list2: { key: string; index: boolean }[] = [
    {
      key: 'string',
      index: true
    },
    {
      key: 'hogehoge',
      index: false
    }
];

// interface
interface list3 {
    id: number,
    season: 'spring' | 'summer' | 'autumn' | 'winter'
}

let obj: list3 = {
    id: 1,
    season: 'spring'
};

上記のコードを下記のサイトにコピペすれば確認できます。
ブラウザ上でTypeScriptを試せるのでオススメです。
Playground

30
25
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
30
25