Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Return Type: void

In TypeScript (using in an Angular project), for a method that returns nothing (void), which of the following is the 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 to leave it blank?

like image 512
Tony Scialo Avatar asked Mar 09 '18 22:03

Tony Scialo


People also ask

What is return type void TypeScript?

According to the TypeScript docs: void represents the return value of functions which don't return a value. Whenever you see a function returning void , you are explicitly told there is no return value. All functions with no return value have an inferred return type of void .

What is return type void?

A void return type simply means nothing is returned. System. out. println does not return anything as it simply prints out the string passed to it as a parameter.

Which has return type void?

Correct Option: E Constructor creates an Object and Destructor destroys the object.

Does void count as a return type?

In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal.


4 Answers

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
like image 139
Ryan Cavanaugh Avatar answered Oct 08 '22 21:10

Ryan Cavanaugh


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

like image 26
Azeem Chauhan Avatar answered Oct 08 '22 19:10

Azeem Chauhan


Another reason is when you are using strict mode.

like image 1
Gilles Avatar answered Oct 08 '22 21:10

Gilles


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);

'''
like image 1
pointyhat Avatar answered Oct 08 '22 21:10

pointyhat