Function Types
Typing function parameters and return values
Introduction
In this lesson, you'll learn about function types in TypeScript. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with typing function parameters and return values.
TypeScript has its own approach to typing function parameters and return values, which we'll explore step by step.
The TypeScript Way
Let's see how TypeScript handles this concept. Here's a typical example:
// TypeScript functions
function add(a: number, b: number): number {
return a + b;
}
const multiply = (a: number, b: number): number => a * b;
function greet(name: string, greeting: string = "Hello"): string {
return greeting + ", " + name;
}
function processItems(
items: string[],
callback: (item: string, index: number) => void
): void {
items.forEach(callback);
}Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
// JavaScript functions
function add(a, b) {
return a + b;
}
const multiply = (a, b) => a * b;
function greet(name, greeting = "Hello") {
return greeting + ", " + name;
}
function processItems(items, callback) {
items.forEach(callback);
}You may be used to different syntax or behavior.
Parameters require type annotations
You may be used to different syntax or behavior.
Return type comes after the parameter list
You may be used to different syntax or behavior.
Callback function types include parameter and return types
You may be used to different syntax or behavior.
void indicates no return value
Step-by-Step Breakdown
1. Parameter Types
Every function parameter should have a type annotation.
function greet(name) { ... }function greet(name: string) { ... }2. Return Types
Add return type after the parameters. TypeScript can often infer this, but explicit types are clearer.
function add(a, b) {
return a + b;
}function add(a: number, b: number): number {
return a + b;
}3. Optional Parameters
Use ? to mark optional parameters. They must come after required parameters.
function greet(name, title) {
return title ? title + " " + name : name;
}function greet(name: string, title?: string): string {
return title ? title + " " + name : name;
}4. Function Type Expressions
You can define the complete type of a function, useful for callbacks.
const handler = (event) => { ... };const handler: (event: Event) => void = (event) => { ... };
// Or as a type alias:
type EventHandler = (event: Event) => void;
const handler: EventHandler = (event) => { ... };Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- Parameters require type annotations
- Return type comes after the parameter list
- Callback function types include parameter and return types
Key Takeaways
- Always type function parameters
- Return types can be inferred but explicit is clearer
- Use ? for optional parameters, = for defaults
- Function types define the shape of callable values