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

206
Vistas
callback function returning value passes type check as void function in TypeScript

I am currently learning TypeScript, and my understanding is that specifying a type for any variable/function should prompt TypeScript to enforce a type-check on its computed value, with the exception of type any.

However, I was surprised today to find that when I created a function with a parameter of type () => void, and the function passed into that function does return something, there was no error from TypeScript. Why is this?

function doSomething(callback: () => void) : void {
  callback();
  // callback is of type `void`, so shouldn't this cause an error?
}
 
doSomething(() => 1); // will return 1 when invoked

Interestingly, when I try to store and use the returned value, I do get an error from TypeScript:

function doSomething(callback: () => void) : void {
  let a = callback();
  console.log(a + 1); // 🚫 Operator '+' cannot be applied to types 'void' and 'number'.
}
 
doSomething(() => 1);

From this example, it appears a is still stored as type void even though the callback function should be returning 1. What's happening under the hood here?

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