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

223
Vistas
Typescript: simpler way to type object to array transformation

I'm converting certain properties from an object to an array.

const result = ({ a, b }: { a: string, b: number }) => [a, b]; // (string | number)[]

However, the standard way always returns an insufficient typescript type (string | number)[]

I have to always specify my output type for it to correctly type the result.

const result = ({ a, b }: { a: string, b: number }): [string, number] => [a, b]; // [string, number]

Is there a simpler way?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Just add as const after the array is created - this is called a const assertion. It indicates that the newly created array is actually a tuple, which it has to be if you're going to count on it having 2 elements where the first is a string and the second is a number.

// readonly [string, number]
const result = ({ a, b }: { a: string, b: number }) => [a, b] as const; 

The only difference between this and the example in your original question is that resulting type will be readonly. There will be no way to alter the value of result unless you do a specific assertion that would allow this.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There's no getting away from the fact that you have to add a little bit of verbosity to get a tuple type back. You can create a helper function which generates tuple types from array literals - whether it is preferable to declaring const or the return signature directly is up to you:

const asTuple = <T extends [any, ...any]>(array: T) => array;

const objectToTuple = ({ a, b }: { a: string, b: number }) => asTuple([a, b]);

const result = objectToTuple({a: "a", b: 1}) // [string, number]
about 4 years ago · Juan Pablo Isaza Denunciar

0

As far as I know, there isn't.

You can do [a, b] as [string, number] but that's basically the same verbosity. Another alternative is giving result a function type, but that's just moving the "problem" here.

Besides, adding that small return type declaration isn't that bad. There isn't much else TS can do, as it can't assume you want a [string, number] by default. That would be far from backwards compatible (or even desired by most people).

about 4 years ago · Juan Pablo Isaza Denunciar
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