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

107
Visualizações
Typescript variadic generic equivalent for return value

I have a function that accepts a generic type arguments, and returns a tuple of arrays of the same types, however this function can accept dynamic amount of those generics. Something like:

function myFunction<T1>(): [Array<T1>] {...}
function myFunction<T1, T2>(): [Array<T1>, Array<T2>] {...}
function myFunction<T1, T2, T3>(): [Array<T1>, Array<T2>, Array<T3>] {...}

and so on.

How do I define a single generic type T and equivalent return value so that I can pass as many different generic as I want?

I have tried doing it in a simple way like:

function myFunction<T extends any[]>(): Array<Array<T[number]>> {...}

However that loses information on what type is stored at which index.

I also tried something similar to this:

function myFunction<T extends any[]>(): {[key in keyof T]: Array<T[key]>} {...}

However calling keyof T on an Array returns other properties besides indexes like "length" etc. which also makes it a little problematic during assignment. Resulting in errors similar to this:

TS2322: Type 'string[][]' is not assignable to type '{ [x: number]: string[]; length: string[]; toString: string[]; toLocaleString: string[]; pop: string[]; push: string[]; concat: string[]; join: string[]; reverse: string[]; shift: string[]; slice: string[]; sort: string[]; splice: string[]; ... 25 more ...; at: string[]; }'.
about 4 years ago · Juan Pablo Isaza
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