🎈

TypeScriptの型定義

2022/06/12に公開

配列にどんな型が入るか最初に定義する

type Menu = {
  key: string,
  label: string,
  icon: ReactElement,
  //ReactElementはreact内で書けるhtmlのようなもの
}
//menuListには Menu[]この配列の型が入る
const menuList: Menu[] = [
  {
    key: 'menu',
    label: 'メニュー',
    icon: (<MdOutlineLightbulb className='mr-4' size='1.5rem'/>),
  },
  {
    key: 'reminder',
    label: 'リマインダー',
    icon: (<BiBell className='mr-4' size='1.5rem'/>),
  },

];

型定義しているおかげで例えばiconのnがなかったらエラーを出してくれる

エラーが出る
 {
    key: 'menu',
    label: 'メニュー',
    ico: (<MdOutlineLightbulb className='mr-4' size='1.5rem'/>),
  },

Discussion

ログインするとコメントできます