Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!