如何在打字稿中获取枚举的最小值/最大值

发布于 2025-01-29 21:12:08 字数 220 浏览 1 评论 0 原文

我在打字稿中有一个枚举,类似于下面的内容:

const enum ControlId {
   ZoomIn = 600,
   ZoomOut,
   Close, 
   Open,
   ....
}

给定数字,我想知道它是否在ControlID范围内,即[600,?],以便我知道它是否属于ControlID类别或不。我想知道是否有办法做到这一点。谢谢。

I have a const enum in TypeScript, something like below:

const enum ControlId {
   ZoomIn = 600,
   ZoomOut,
   Close, 
   Open,
   ....
}

Given a number, I want to know if it is within the range of ControlId, which is [600, ?], so that I can know if it falls into the category of ControlId or not. I am wondering if there is a way to do that. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2025-02-05 21:12:08

我认为您是说您有一个运行时值,您想知道它是否是 ControlID 的有效成员(您已经说过要进行“范围检查”,但是数字枚举不一定是连续)。

const Enum 没有运行时的存在,因此您无法编写运行时代码来验证一个数字以查看它是否是 const enum enum 的有效成员。

如果 enum 不是 const :我可以做到这一点,您可以做到这一点

enum ControlId {
    ZoomIn = 600,
    ZoomOut,
    Close, 
    Open,
    // ....
}

function assertIsControlId(value: number): asserts value is ControlId {
    if (!(value in ControlId)) {
        throw new Error(`Invalid ControlId value ${value}`);
    }
}

:我已经将其作为类型的断言函数完成内联表达。基本上,如果值为数字,并且该值(当转换为字符串时,由运算符中隐式> operator中隐式)是 ControlIDID 的有效属性名称,则是有效的成员 ControlID

如果您确实是指最小/最大,即使不能保证枚举值是连续的,您也可以这样做:

function getMinMaxOfEnum(e: object) {
    const values = Object.keys(e).map(k => k === "" ? NaN : +k).filter(k => !isNaN(k)).sort((k1, k2) => k1 - k2);
    return [values[0], values[values.length - 1]];
}

(对于 e 可能有更好的类型代码>对象。

从您传递的对象中获取所有键和阵列的最后一个元素。

另外,使用 Math.min MATH.MAX 而不是排序:

function getMinMaxOfEnum(e: object) {
    const values = Object.keys(e).map(k => k === "" ? NaN : +k).filter(k => !isNaN(k));
    return [Math.min(...values), Math.max(...values)];
}

Playground link

I think you're saying you have a runtime value and you want to know if it's a valid member of ControlId (you've said you want a "range check," but numeric enums aren't necessarily contiguous).

A const enum has no runtime existence, so you can't write runtime code to validate a number to see if it's a valid member of a const enum.

You can do it if the enum isn't const:

enum ControlId {
    ZoomIn = 600,
    ZoomOut,
    Close, 
    Open,
    // ....
}

function assertIsControlId(value: number): asserts value is ControlId {
    if (!(value in ControlId)) {
        throw new Error(`Invalid ControlId value ${value}`);
    }
}

There I've done it as a type assertion function, but that could be a type guard function instead, or just an inline expression. Basically, if the value is a number, and the value (when converted to string, implicitly by the in operator) is a valid property name for ControlId, it's a valid member of ControlId.

If you really mean you want the min/max, even though the enum values are not guaranteed to be contiguous, you can do that like this:

function getMinMaxOfEnum(e: object) {
    const values = Object.keys(e).map(k => k === "" ? NaN : +k).filter(k => !isNaN(k)).sort((k1, k2) => k1 - k2);
    return [values[0], values[values.length - 1]];
}

(There's probably some better type for e than object. And in modern environments, values[values.length - 1] could be values.at(-1).)

That gets all of the keys from the object you pass in, converts them to number if possible, removes the ones that didn't convert successfully (so now we have an array of enum values), sorts the resulting array, and returns the first and last elements of the array.

Alternatively, use Math.min and Math.max instead of sorting:

function getMinMaxOfEnum(e: object) {
    const values = Object.keys(e).map(k => k === "" ? NaN : +k).filter(k => !isNaN(k));
    return [Math.min(...values), Math.max(...values)];
}

Playground link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文