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

111
Views
Creating string literal union from a property in an array of objects

Is it possible to get a literal string union type of a static array of object's specific property?

For example, I want to expose IconName as a type of literal strings from which is inferred from an array of objects.

I've seen it has been done to convert a read-only array to a string literal union, but not through an array of objects.

I don't think it's possible since we require some runtime interpretative functionality to access the array's values. But here's the example I'm trying to work through this svelte example https://codesandbox.io/s/svelte-typescript-forked-ef75y:

<script lang="ts">
interface Icon {
  name: string
  color: string
}

const icons: readonly Icon[] = <const>[
  {
    name: "close",
    color: "red"
  },
  {
    name: "open",
    color: "blue"
  }
];

const tuple = <K extends string[]>(...arr: K) => arr;
const iconNames = tuple(...icons.map(i => i.name));

type IconName = typeof iconNames[number];

export let name: IconName;

let displayIcon = icons.find((e) => e.name === name);
</script>


<h1>{displayIcon.color}</h1>
about 4 years ago · Juan Pablo Isaza
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!