Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

70
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda