- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
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.
๐งช 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).
