TypeScript · Senior · Question 44 of 75
Why is it recommended to annotate return types of exported functions in a library?
- ✓Correct answer: It speeds up checking and pins down the public contract
- 2Without an annotation TypeScript can't infer the result type
- 3It's required by the ECMAScript modules specification
Explanation
Inferring a complex type has to be repeated wherever the function is used, and declarations get bulky. An explicit type keeps the contract stable: implementation changes don't "leak" into the API. With isolatedDeclarations (TS 5.5), annotations on exports become mandatory.