Closed
Description
Suggestion
π Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
JSDoc support for String array to Object key
π Motivating Example
/**
* @template T
* @param {T[]} obj
* @returns {Readonly<{[T]: number}>}
*/
function Flags(obj) {
const _obj = {}
_.forEach(obj, (key, index) => {
_obj[key] = 1 << index
})
return Object.freeze(_obj)
}
const TEST = Flags([
'A',
'B',
'C',
'D'
])
// We want to
// TEST.
// -----------
// | A |
// | B |
// | C |
// | D |
// -----------
π» Use Cases
In the above example, I can use bit enumeration
/**
* @param {number} enums
* @param {number} flags
*/
function hasFlag(enums, flags) {
return flags === (enums & flags)
}
cosnt test = TEST.A | TEST.C | TEST.D
console.log(hasFlag(test, TEST.A)) // true
console.log(hasFlag(test, TEST.B)) // false
console.log(hasFlag(test, TEST.C)) // true