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

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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