TypeScript · Senior · Question 44 of 75

Why is it recommended to annotate return types of exported functions in a library?

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.

Further reading

Problem with a question

TypeScript · Senior · question 44

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz