#Generic list of functions

5 messages · Page 1 of 1 (latest)

hallow dust
#

I have the following class method:

or<T extends (() => any)[]>(rules: T): ReturnType<T[number]> {
  for (const rule of rules) {
    try {
      return this.call(rule);
    } catch (_) {
      continue;
    }
  }
  throw new Error("no rule matched");
}

I want it to work such that e.g. or([() => "a", () => 1]) would be of type string | number. Currently the inference is not working correctly and I'm just getting any.

Any ideas on getting this to work?

#

Note the functions don't actually return strings and numbers

ionic gale
#

It should work though.

vivid cryptBOT
#
declare function or<T extends (() => any)[]>(rules: T): ReturnType<T[number]>

const result = or([() => 'hello', () => 42])
//    ^? - const result: string | number
hallow dust
#

ah yeah I had the issue wrong it's something else