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

310
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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