In TypeScript, to work with a subset of items in an enum, we typically do the following:
enum Operator {
EQUAL = 'EQUAL',
NOT_EQUAL = 'NOT_EQUAL',
LESS = 'LESS',
GREATER = 'GREATER',
}
type EqualOperators = Exclude<Operator, Operator.LESS | Operator.GREATER>
I'm wondering if there's a way to convert EqualOperators
from a type
to an enum
, so that I can use it directly in a script?
By "use it in a script," I mean being able to access the enum
values within a JavaScript function. This is not possible with types
. For example:
enum Operator {
EQUAL = 'EQUAL',
NOT_EQUAL = 'NOT_EQUAL',
LESS = 'LESS',
GREATER = 'GREATER',
}
function getOperator() {
if (Operator.EQUAL) {
return "=";
}
if (Operator.NOT_EQUAL) {
return "!=";
}
throw new Error('Invalid operator');
}
Any suggestions or solutions would be greatly appreciated. Thank you!