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

124
Visualizações
Get the value of a parameter and make it the type of another parameter

The behavior I'm looking for can be implemented with overloading:

function fun(elType: "input", params: Input): number;
function fun(elType: "swtich", params: Switch): number;
function fun(){
  return 2
}

Where if I run:

fun("swtich", SOMETHING_OF_SHAPE_SWITCH) //compiles
fun("input", SOMETHING_OF_SHAPE_INPUT) // error
fun("input", SOMETHING_OF_SHAPE_INPUT) //compiles

I was wondering if it can be done by checking the parameter directly:

interface Input {
  a: 2
}

interface Switch {
  b: 2
}

type ElType = {
  input: Input,
  switch: Switch,
}

const fun = <T extends keyof ElType>(elType: keyof ElType, params: ElType[typeof elType]) => { }

That's as close as I got but it allows both:

fun("input", {
  b: 2
})


fun("input", {
  a: 2
})

It seems that I'm close, but I'm a) not quite sure what's happening in this last example and b) not sure how to achieve the desired bahaviour.

It seems what I want to do is:

const fun = <T extends keyof ElType>(elType: keyof ElType, params: ElType[elType]) => {}

But I get the error: type 'elType' cannot be used as an index type.

Here's a tsplayg4round I'm using if you want to play around.

I've seen several libraries implement this pattern so I was wondering what's the most idiomatic way of doing it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You've defined the generic T but haven't used it! TypeScript doesn't know what to do with it so it ignores it.

<T extends keyof ElType>(elType: T, params: ElType[T]) => ...

Using it here allows TypeScript to infer what T is from elType, and once it knows what T is, you can use ElType[T] to get the right params.

about 4 years ago · Juan Pablo Isaza Relatório
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