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

347
Vistas
How to infer function overloading TS Types, when args are functions with optional args

I want to implement a pattern similar to React.useState(). Basically, the function would accept one of 2 things:

  • A {string} value to update the state to.
  • A {function} that accepts the previousState and produces a new one.

Here is an example not that complex. The interface din:

interface OverloadedFn<TArgs>  {
    (args: TArgs): void;
    (setter: (args: TArgs) => TArgs): void;
}

const overloadedFn: OverloadedFn<{ arg1: number }> = (arg) => {
    if (typeof arg === 'function') {
      ...
    } else {
      ...
    }
}

I can write the overload, with multiple function, with type or with interface, but the args, will never get typed. I even removed the Generic. Any ideas?

TS7006: Parameter 'arg' implicitly has an 'any' type.

Here is a minimal reproducible example, in TS Playgound: https://www.typescriptlang.org/play?ts=4.1.5#code/JYOwLgpgTgZghgYwgAgPIDdoBsD2cAmE+AYiADwAqAglAOYDOAfMgN4CwAUMsgBRx30AXMmoCAlMPpgooWgG5O3HvQhhIUYXwHDRDMcgC8zXfQnIpMkPM4BfTpwQ4QU5DkxRcBIqWEZseQhJyFmR+WgBGYRAAVwBbACNoZBtmA14w00Nmdi5kYBheMABPAAcIHAKMwwM0gHIYaJAEMGAnWv0c7i7kKFVoqBBkWvDaxWTkCCwVVjHu3rB+wdqAJlHcuw4bOSA

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