【发布时间】:2020-04-29 00:58:29
【问题描述】:
我在 jsdoc 中使用 typescript,并试图将一个变量限制为我在数组中拥有的一组已知值中的一个。
我知道我可以这样做:
/** @type {'one'|'two'|'three'} */
let v = 'four';
// ==> Error, type 'four' is not assignable to type 'one'|'two'|'three'
就我而言,我在数组附近有所需的值。为了避免重新输入,我想以某种方式引用它们,但我不知道这是否可能。我想要这样的东西:
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...
有没有办法做到这一点?
【问题讨论】:
标签: typescript jsdoc