Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

71
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda