I'm using typescript in jsdoc, and am trying to constrain a variable to one of a known set of values that I have in an array.
I know I can do it like this:
/** @type {'one'|'two'|'three'} */
let v = 'four';
// ==> Error, type 'four' is not assignable to type 'one'|'two'|'three'
In my case, I have the desired values nearby in an array. To avoid retyping, I'd like to somehow reference them, but I don't know if it's possible. I'd like something like this:
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 some way to do this?