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

68
Views
ts conditional generic type

I have a generic type:

type TResult<T> = {
  data: string[];
  key: T[];
};

and I want to pass T conditionally, I provided a simple example, I want to set the type depending on the key variable that I get it's value from the provided funtion.

Do you have any idea how I can do that? thanks in advance.

UPDATE!: full-example

type TKey = 'foo' | 'bar' | 'baz'

type TType1 = {
    firstName: string
    lastName: string
}

type TType2 = {
    citry: string
    address: string
} 

type TType3 = {
    age: number
    dob: Date
} 

type TResult<T> = {
    data: string[];
    key: T[]
}

function getQuery():TKey {
    const keys: TKey[] = ['bar', 'baz', 'foo']

    return keys[Math.floor(Math.random() * 3)]
}

const key = getQuery()

const result:TResult< /** I want to set the generic type depending on key */ > 
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!