I'm encountering an issue with TypeScript that I can't seem to resolve. My situation involves a Comparator
object that consists of various functions.
type GenericCompareFn = (condValue: any, browserValue: any) => boolean
type IsSetCompareFn = (browserValue: any) => boolean
export type ComparatorType = {
equals: GenericCompareFn
not_equals: GenericCompareFn
contains: GenericCompareFn
not_contains: GenericCompareFn
lower: GenericCompareFn
greater: GenericCompareFn
between: GenericCompareFn
in: GenericCompareFn
not_in: GenericCompareFn
is_set: IsSetCompareFn
is_not_set: IsSetCompareFn
}
export type Operation = keyof ComparatorType
Now, the issue stems from the evaluate
function which is supposed to invoke a specific method from the Comparator
based on the provided Operation
. However, the type checking fails even when wrapped inside a switch/case
block:
const evalFn = Comparator[operation]
if (!evalFn)
// Since I'm handling external input, I cannot trust the operation variable completely, hence the Exception
throw new ConditionEvaluationError(
`The specified operation ${operation} does not exist`
)
switch (operation) {
case "is_set":
case "is_not_set":
// This is where the error occurs - "Expected 2 arguments, but got 1. ts(2554)"
return evalFn(attribute.getBrowserValue())
default:
return evalFn(value, attribute.getBrowserValue())
}