Utilizing TypeScript with JSDoc poses a challenge as I aim to restrict a variable to one of the known values stored in an array.
I am aware that it can be achieved like so:
/** @type {'one'|'two'|'three'} */
let v = 'four';
// ==> Error, type 'four' is not assignable to type 'one'|'two'|'three'
In my scenario, the desired values are already defined in an array nearby. To prevent redundancy, I wish to reference them somehow, although I am uncertain if it's feasible. Something along these lines would be ideal:
const OPTIONS = ['one', 'two', 'three'];
/** @type {string<Options>} */
let v = 'four';
// ==> Desired -- Error, type 'four' is not assignable to type 'one'|'two'|'three'
// ==> but that doesn't actually work...
Is there any method to accomplish this?