Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda