Picci
Picci

Reputation: 17762

Why eslint ignored type inference of TypeScript

TypeScript can infer the return type of a function and this is very neat in my opinion.

Why then eslint, by its default configuration, gives a warning if an explicit return type is not specified for a function?

Upvotes: 1

Views: 895

Answers (2)

Evert
Evert

Reputation: 99543

If your function should always return number, but you added a new condition that returns undefined, being explicit about the return type lets you decide:

  1. The return type should be updated and all callers should handle the undefined case.
  2. Returning undefined was a mistake, and perhaps it was better to throw an error

The point is that explicit return types force the implementer to think about what they intend to return.

Upvotes: 2

H3AR7B3A7
H3AR7B3A7

Reputation: 5261

Even though TypeScript can infer types, it is often not clear at first sight to the developer what the return type of a function is.

I can only guess the creator thought the same. You can easily change the default behavior, turning it "off" entirely or just for inline callbacks.

Documentation

Another valid question on SO

Upvotes: 0

Related Questions