Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

59
Vistas
Typescript generics and returning functions

I have a function that accepts another function as an argument, and returns a function having the same input type signature, but void instead of whatever output type the inner function had. The actual code does some other things, but here's the dumbed down version of it. I just can't seem to get the types right. Here's what I have so far (I'm aware it's wrong):

export function foo<Type>(func : (_ : Type) => any) {
  return function bar(...args : Type) : void {
    func.apply(null, args)
  }
}

The first line doesn't really capture what I want it to - the entire argument list of func should be Type, not just the first argument. I can't seem to be able to do so, though. (Note that Type[] doesn't capture the intended use, since not all arguments need to be the same type.) Is this even possible given the generics that Typescript offers?

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos