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

176
Vistas
How do I enumerate options assigned to a type?

A Node.js package I'm using has a method that takes an argument required to be of a type that via IntelliSense I can see is defined with a typescript declaration in the form:

export declare type ArgType = 'A' | 'B' | 'C';

I'd like to call the package method using each permissible value for this argument in turn; in other words, something in the form:

['A','B','C'].forEach((v) => packageObject.packageMethod(v));

except I'd like to avoid hard-coding a static copy of the list of options.

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

0

If you have the ability to modify the package, you can derive the union of strings from a readonly array of string literals, and also export the array for use in runtime code:

TS Playground link

// exported as values (usable at runtime)
export const stringArgs = ['A', 'B', 'C'] as const;

// exported as union of strings (not usable at runtime)
export type ArgType = typeof stringArgs[number]; // "A" | "B" | "C"

export function fn (arg: ArgType): void {
  // ...
}

Otherwise, you'll have to parse the values using the compiler API, as mentioned by @ASDFGerte.

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