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

309
Visualizações
Typescript conditional types depending on occurrence in dictionary

I'm trying to type a function with a rather complex type signature. It accepts two dictionaries args and funcs, where funcs maps keys to functions that transform type A to B, and args maps a superset of those keys to: A if the key occurs in funcs, and to B otherwise. I.e., if a function is defined for a certain key, use the function to map the value from the same key from the other dictionary, if no corresponding function exists just use the value itself.

I've got the following so far, but that doesn't seem to work:

function foo<Properties>(
  args: { [P in keyof typeof funcs]: Parameters<typeof funcs[P]>[0] } & {
    [P in Exclude<keyof Properties, keyof typeof funcs>]?: Properties[P];
  },
  funcs: { [P in keyof Properties]?: (attributes: any) => Properties[P] }
) {}

interface Bar {
  x: number;
  y: string;
}

foo<Bar>(
  {
    x: "a",
    y: 1,
  },
  {
    y: (a: string) => "a",
  }
);

In this case, I would expect to see two errors: one indicating that x should map to a number in the first dictionary, and the second indicating that y should be a string in the first dictionary. It seems I'm overestimating the power of TS - is this intended behavior even possible?

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

0

Interesting problem!

I think this achieves your intended outcome:

type Func<I, O> = (arg: I) => O
type Funcs<T> = { [K in keyof Partial<T>]: Func<K, T[K]> }

declare function converter<Properties>(
  args: Properties,
  funcs: Funcs<Properties>
): void;

interface Bar {
  x: number;
  y: string;
}

converter<Bar>(
  {
    x: 1,
    y: "foo",
  },
  {
    y: (a: string) => "2",
  }
);
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