Break Functions
๐จโ๐ผ Fantastic! You now know how to write type-safe functions.
You learned:
- ๐งฑ Function declarations define named, reusable functions
- ๐ฅ Parameter types specify what a function accepts
- ๐ค Return types specify what a function produces
- ๐ฎ Type inference figures out return types automatically
- โ๏ธ Explicit return types document your intent and catch mistakes
- โก๏ธ Arrow functions for concise callbacks and implicit returns
๐ฆ When to use each function form:
| Context | Use | Example |
|---|---|---|
| Top-level named functions | Function declaration | function processOrder() { ... } |
| Callbacks | Arrow function | items.map((n) => n * 2) |
| Single-expression funcs | Arrow (implicit) | const double = (n) => n * 2 |
// Function declaration for top-level functions
function processOrder(items: CartItem[]): OrderResult {
// Complex logic...
}
// Arrow functions for callbacks
const doubled = items.map((item) => item.price * 2)
// Arrow function with implicit return
const isExpensive = (price: number): boolean => price > 100
Next up: Special return typesโ
void and never!Test Your Knowledge
Retrieval practice helps solidify learning by actively recalling information. Use this prompt with your AI assistant to quiz yourself on what you've learned.
Please quiz me on exercise 5 using the epicshop MCP server. Call the get_quiz_instructions tool with exerciseNumber "5" to get the quiz instructions, then quiz me one question at a time.