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

102
Views
Extract string literal union from array of classes based on constructor parameters

I am trying to get a string literal union based on values passed to an array of classes via constructor.

For example, the array looks like this:

const myArray = [
  new MyClass({
    key: "key1", 
  }),
  new MyClass({
    key: "key2", 
  }), 
]

And I'm trying to construct a mapped version of it (for lookup) or a string literal union:

type MyUnionType = "key1" | "key2"  

The primary reason for this is to ensure that any lookups use available keys assigned to the class.


getClass('key1') // all good 

getClass('notAKey') // error

However all solutions I've seen so far include some form of redundancy in either passing the key as a generic or creating an object with the same keys.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this:

type MyUnionType = {
  [K in keyof typeof myArray]:
    (typeof myArray)[K] extends MyClass<infer T> ? T : never
}[number]

This is a mapped type that goes over each key in the array, and then infers the generic parameter from each member of myArray.

This assumes a class like:

class MyClass<T extends string> {
  constructor(options: { key: T }) {}
}

Which you would use like:

const myArray = [
  new MyClass({
    key: "key1", 
  }),
  new MyClass({
    key: "key2", 
  }), 
] as const

function getClass(input: MyUnionType) {}
getClass('key1') // works
getClass('notAKey') // error

Playground


Or, parameterized for more flexibility:

type MyUnionType<T extends readonly MyClass<string>[]> = {
  [K in keyof T]:
    T[K] extends MyClass<infer U> ? U : never
}[number]

function getClass(input: MyUnionType<typeof myArray>) {}
getClass('key1') // works
getClass('notAKey') // error

Playground

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!