2

I am trying to create a relatively simple function signature like the following:

function<U>(...args:U[]):U {/* body */}

I want typescript to combine the types of the args passed into a union instead of simply inferring U as the type of the first argument. Please how would I go about this?

Seyi Shoboyejo
  • 489
  • 4
  • 11
  • I know I can specify the union in the call to the function: ... But this quickly becomes very verbose and unwieldy for a large number of different types – Seyi Shoboyejo Aug 04 '22 at 18:26
  • 1
    Heuristics prevent `U` from being inferred as a union. You could instead do [this](https://tsplay.dev/w2PDbm) where the array type is generic and then you can index into it to get your desired union. Does that fully address your question? If so, I could write up an answer; if not, what am I missing? – jcalz Aug 04 '22 at 18:27
  • @jcalz I think it could. I looked at the suggestion linked to by the other comment; but the U[number] ended up being just the common denominator instead of the union of the types. – Seyi Shoboyejo Aug 04 '22 at 18:45
  • @jcalz exactly your linked example worked like a charm! Post it as the answer; so I can mark the question as resolved. Thanks! – Seyi Shoboyejo Aug 04 '22 at 18:51

1 Answers1

2

The problem you're having is that the following code produces a compiler error:

function f<U>(...args: U[]): U { 
  return args[Math.floor(Math.random() * args.length)] 
}

const a = f("string", 123, true); // error!
//                    ~~~ <-- error! 123 is not a string
// U inferred as string
// const a: string

Compiler heuristics prevent inferring a union here because often people want that error to happen. See Why isn't the type argument inferred as a union type? for more information.

There is an open feature request at microsoft/TypeScript#44312 asking for some way to tell the compiler that you want a union instead of an error. For now this is not possible, so if you want similar behavior you need to use workarounds.

One common workaround is to change the function so that it is generic not in the type U of the elements of args, but in the array type T of args itself. From there, you can index into that type with number to get the element type. That is, if T is an arraylike type, then T[number] is the element type (since that's the type you get when you index into the array with a number):

function f<T extends any[]>(...args: T): T[number] { 
  return args[Math.floor(Math.random() * args.length)] 
}

const a = f("string", 123, true);
// T inferred as [string, number, boolean]
// const a: string | number | boolean

Now there's no error, and the type of a is the union you wanted.

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360
  • This common workaround has been perfect for my use case; thanks! I don't even see it as a workaround now but as correct behaviour; since I actually want to infer the entire list of arguments as one thing instead of as a list of things... – Seyi Shoboyejo Aug 04 '22 at 19:10
  • The point I was trying to make with the earlier comment was that by following the logical order we first need to have the entire args array; then type it; then derive the element type from there. That just seems like correct semantics to me rather than a workaround... – Seyi Shoboyejo Aug 05 '22 at 20:13