Home > Enterprise >  Get Typescript to infer a union type
Get Typescript to infer a union type

Time:08-05

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?

CodePudding user response:

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

  • Related