Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Return type is one of the value from array

I have function which takes an array in an object as parameter. It calculates and return one of the value from that array. Is there a way to document the return type from one of the values from array dynamically.

Say

function random(params) {
  // calculate
  int random = 2; // this will change
  return params.values[random];
}

random({
  prop1: 'lorem ipsum',
  prop2: 3.444,
  values: [ 'value1', 'value2', 'value3', 'value4' ]
})

So random jsdoc should be dynamic but should look something like

/**
 * Some radom generator.
 * @param {object} props - Function parameters.
 * @param {string} props.prop1 - Some prop1.
 * @param {number} props.prop1 - Some prop2.
 * @param {string[]} props.values - List of values to pick.
 * @returns {('value1'|'value2'|'value3'|'value4')}
 */

The last return should somehow be dynamic

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The following would do the trick:

/**
 * Some random generator.
 * @template {string} T
 * @param {object} params - Function parameters.
 * @param {string} params.prop1 - Some prop1.
 * @param {number} params.prop2 - Some prop2.
 * @param {T[]} params.values - List of values to pick.
 * @returns {T}
 */

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!