Functional TypeScript
In TypeScript, a function is the fundamental way to call logic. TypeScript adds Type Safety to JavaScript functions by allowing you to define the exact types of the Parameters and the Return Type.
typescript
Parameter Annotations
Functions can have mandatory, optional, or default parameters.
- Mandatory: Default.
- Optional (
?): Might be missing (Value isundefined). - Default (
=): A value to use if the argument is missing.
typescript
Arrow Functions (Modern ES6)
Arrow functions are the standard for modern React and Next.js development. Their syntax is cleaner and they automatically handle the
this keyword correctly.typescript
Function Overloading
In TypeScript, you can provide multiple versions of the same function, as long as they have the same implementation. This is useful for functions that can handle 2+ different types of data.
typescript
Summary
| Type | Examples | Description |
|---|---|---|
| Parameter | (x: string) | Input variable |
| Return | (): number | Data sent back to caller |
| Optional | (x?: string) | Parameter can be missing |
| Default | (x = 10) | Value if parameter is missing |
| Rest | (...args: number[]) | Indefinite number of inputs |
| Void | void | Used for functions that return NOTHING |
| Best For | Logic, callbacks, events, and reusability | |
| Arrow | Modern, concise syntax (TS default!) | |
| Signature | The name + inputs (Defines the function's type!) | |
| Callback | Passing a function AS a parameter | |
| Type Safe | TS catches "Wrong data type" errors! | |
| Overload | One function, multiple type signatures | |
| Async | Use Promise<T> for return types of async functions! | |
| Nested | Defining a function inside another function | |
| Closure | Functions "remember" their parent variables | |
| Recursive | A function that calls itself (Use carefully!) | |
| Implicit | TypeScript often guesses the return type automatically! | |
| Interface | You can describe a function with an interface! | |
| Generic | Functions that work with any type (See Generics section!) | |
| Hoisting | Normal functions are "hoisted" to the top; Arrows ARE NOT! | |
| Bound | Arrows automatically "bind" the this keyword | |
| Pure | Functions that always return the same result for same input | |
| Security | Prevents calling a function with too MANY or too FEW arguments | |
| Tooling | Perfect autocomplete for function calls in VS Code | |
| Documentation | Types acting like live documentation for your logic |