Ad space (banner)
Lesson 5 / 20

Function Types (Parameters and Return Values)

This lesson covers typing a function's parameters and return value in TypeScript. It's for anyone searching "TypeScript function type annotation."

When defining a function, you can type each parameter as paramName: type, and the function as a whole with ): return-type {. This makes it obvious at a glance what a function accepts and what it returns.

The example defines function greet(name: string): string. name takes a string, and the function as a whole returns a string.

A common early mistake is forgetting to write the return type, then not noticing an unintended type is actually being returned. Writing the return type explicitly means TypeScript automatically catches any mismatch if you later change the function's internals.

In real projects, being able to tell how to use a function just by looking at its signature (parameter and return types) makes using someone else's function far less nerve-wracking.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)