Chumpocomon
Chumpocomon

Reputation: 733

Does Typescript automatically detect void functions?

I’m a newbie in Typescript and in programming in general.

Instead of this:

function greet(greeting: string): void;

Can I use something like this? Is there any kind of inference?

function greet(greeting: string)

Upvotes: 0

Views: 55

Answers (1)

emeraldsanto
emeraldsanto

Reputation: 4741

Yes, in 95% of cases TypeScript will correctly infer the return type of a function. Exceptions to this include "tuples", which will be inferred as arrays, and anonymous objects which you may have already declared a type for.

In this case, no need to explicitly declare the return type as void, except for clarity of course.

Upvotes: 3

Related Questions