0

I have been looking for a way to document this in js:

function foo(param1: 'opt1' | 'opt2') { ... }

But found nothing because I don't know how can I search for it and didn't find any example alike to what I want to do in JSDoc

I have tried with:

/**
* @param {string} param1 - Receives either 'opt1' | 'opt2'
*/
function foo(param1) { ... };

But I think there would be a better way to specify if param1 receives a string that's either 'these' or 'that'. Another solution than only put in the param description what you can pass as value like shown in my example.

Matt
  • 68,711
  • 7
  • 155
  • 158

1 Answers1

1

You can document your function this way in case of strings:

/**
* @param {('opt1' | 'opt2')} param1
*/
function foo(param1) { };

Using the same method you can also handle integer values

/**
* @param {(1 | 2)} param1
*/
function foo(param1) { };

This will allow editors to give suggestions using the documented values like following

enter image description here

myassir
  • 76
  • 4