object-as-dictionary
Use of JS objects as dictionaries is common but error prone problem given the hazards of inherited properties and mutable __proto__
. There for it's typically better to use objects created like Object.create(null)
for dictionaries. But then when you also have entries for the dict you wind up with Object.assign(Object.create(null), {x: 1, y: 2})
finally if you type check your js code boilerplate even greater.
This is miniature library deal with this boilerplate for you:
const ages = Object // => null'__proto__' in ages // => falseages__proto__ // => undefined'constructor' in ages // => falseagesconstructor // => undefined
And if you happen to type check your JS code with flow you can take further advantage of this library:
/* @flow */ const ages:Dictionary<number> = agesKate = 'two' // flow check//// src/index.js:10// 10: ages.Kate = 'two'// ^^^^^ string. This type is incompatible with// 6: const ages:Dictionary<number> = dictionary({// ^^^^^^ number