Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda