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

256
Vistas
How to convert typescript types of strings to array of strings?

I've this type:

type myCustomType = "aaa" | "bbb" | "ccc";

I need to convert it to an array like this:

["aaa", "bbb", "ccc"]

How can I do this in typescript?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can't do it with string literal but you can do it with an enum

enum MyEnum {
  "aaa",
  "bbb",
  "ccc",
}

Object.keys(MyEnum).forEach(key => {
  console.log(key);
})

Repro here : https://stackblitz.com/edit/typescript-uqvach

over 4 years ago · Santiago Trujillo Denunciar

0

Types do not exist in emitted code - you can't go from a type to an array.

But you could go the other way around, in some situations. If the array is not dynamic (or its values can be completely determined by the type-checker at the time it's initialized), you can declare the array as const (so that the array's type is ["aaa", "bbb", "ccc"] rather than string[]), and then create a type from it by mapping its values from arr[number]:

const arr = ["aaa", "bbb", "ccc"] as const;
type myCustomType = typeof arr[number];

Here's an example on the playground.

over 4 years ago · Santiago Trujillo Denunciar

0

For example:

  const themeMode = ['light', 'dark'] as const;
  type MainTheme = typeof themeMode[number];
  const values = [...themeMode];

  // iterate on themeMode as const assertion
  values.map((theme) => console.log(theme));
over 4 years ago · Santiago Trujillo 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