Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSDoc support for String array to Object key #45902

Closed
5 tasks done
imba97 opened this issue Sep 16, 2021 · 4 comments
Closed
5 tasks done

JSDoc support for String array to Object key #45902

imba97 opened this issue Sep 16, 2021 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@imba97
Copy link

imba97 commented Sep 16, 2021

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
@andrewbranch andrewbranch added the Question An issue which isn't directly actionable in code label Sep 16, 2021
@andrewbranch
Copy link
Member

Yepβ€”added in #45464

@imba97
Copy link
Author

imba97 commented Sep 17, 2021

Thank you very much

@imba97 imba97 closed this as completed Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants