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

323
Vistas
Typescript Return Type: void

In Typescript (using in an Angular project) for a method that returns nothing (void), which of the below is best practice?

onSelect(someNumber: number): void {

}

OR

onSelect(someNumber: number) {

}

I've seen it both ways in different examples and wasn't sure if it is better to add the return type as void or leave it blank?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

It's entirely personal preference whether you explicitly annotate a method's return type or not, especially for a trivial type like void.

Reasons you might add : void:

  • Improves clarity - other devs don't have to read the method body to see if it returns anything
  • Safer - if you e.g. move code from another function into this one that has a return expr; statement in it, TypeScript will flag this mistake

Reasons you might not:

  • Brevity - if it's clear from context what the return type should be (e.g. getLength() is almost certainly returning number), then a return type annotation is slightly noisy
  • Flexibility - if you're prototyping this code and aren't sure yet what you want the return type to be, leaving it inferred lets you change it more easily
over 4 years ago · Santiago Trujillo Denunciar

0

Remember the Rule : "void is the return type of a function/method that doesn’t explicitly return anything"

Whether your use "void" or not as return type in function/method, its automatically infer to "void" return type if there is no explicit return type

over 4 years ago · Santiago Trujillo Denunciar

0

Another use-case which benefits from being explicit with 'void' return type IMO is one-line arrow functions.

// the second method has no body block, and implicitly returns the 
// size of the internal array, which may not be the desired outcome
// explicitly stating a :void return type would flag the bottom one
// as an error
add = (v: string):  => {this.list.push(v);};
addAndReturnLengthOfList = (v: string):  => this.list.push(v);

'''
over 4 years ago · Santiago Trujillo 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