Skip to content

JSDoc support for String array to Object keyΒ #45902

Closed
@imba97

Description

@imba97

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions