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

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

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 Report

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 Report

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 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!