Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Get Typescript to infer a union type

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!