[TypeScript] Recordや{[key]: string}よりnew Mapを使う理由

Recordや{[key]: string}よりnew Mapを使う理由

const obj: Record = { a: 'a' };
// itemはstringとして推論される、実態はundefined
const item = obj.b;
// Cannot read properties of undefined (reading 'toUpperCase')
item.toUpperCase();

const hoge = new Map();
hoge.set('hoge', 1);
const a = hoge.get('hoge');
 // -> number | undefinedになる
 //use noUncheckedIndexedAccessでカバーできる

playground