Reputation: 17762
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
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:
undefined
case.undefined
was a mistake, and perhaps it was better to throw an errorThe point is that explicit return types force the implementer to think about what they intend to return.
Upvotes: 2
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.
Upvotes: 0