Functions
Defining and calling functions with types
Introduction
In this lesson, you'll learn about functions in TypeScript. Coming from Go, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Go, you're familiar with defining and calling functions with types.
TypeScript has its own approach to defining and calling functions with types, which we'll explore step by step.
The TypeScript Way
Let's see how TypeScript handles this concept. Here's a typical example:
// Basic function
function add(a: number, b: number): number {
return a + b;
}
// Returning a tuple (no multiple return)
function divide(a: number, b: number): [number, Error | null] {
if (b === 0) return [0, new Error("division by zero")];
return [a / b, null];
}
// Rest parameters (variadic)
function sum(...nums: number[]): number {
return nums.reduce((acc, n) => acc + n, 0);
}
// Arrow function
const double = (x: number): number => x * 2;Comparing to Go
Here's how you might have written similar code in Go:
// Basic function
func add(a, b int) int {
return a + b
}
// Multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
// Variadic function
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
// First-class function
double := func(x int) int { return x * 2 }You may be used to different syntax or behavior.
Go supports true multiple return values; TypeScript returns tuples (arrays)
You may be used to different syntax or behavior.
Go uses variadic ...T; TypeScript uses rest parameters ...T[]
You may be used to different syntax or behavior.
TypeScript arrow functions are more concise than Go's func literals
You may be used to different syntax or behavior.
Go's error return pattern → TypeScript uses exceptions or Result tuples
Step-by-Step Breakdown
1. Basic Signature
TypeScript puts types after parameter names with a colon, and the return type comes after the closing parenthesis.
func add(a, b int) intfunction add(a: number, b: number): number2. Multiple Returns
Go natively supports multiple return values; TypeScript emulates this with destructured arrays.
func minMax(arr []int) (int, int) {
return arr[0], arr[len(arr)-1]
}
min, max := minMax(nums)function minMax(arr: number[]): [number, number] {
return [arr[0], arr[arr.length - 1]];
}
const [min, max] = minMax(nums);3. Arrow Functions
TypeScript arrow functions are the idiomatic way to write short callbacks, equivalent to Go's anonymous func literals.
transform := func(x int) int { return x * 2 }const transform = (x: number): number => x * 2;Common Mistakes
When coming from Go, developers often make these mistakes:
- Go supports true multiple return values; TypeScript returns tuples (arrays)
- Go uses variadic ...T; TypeScript uses rest parameters ...T[]
- TypeScript arrow functions are more concise than Go's func literals
Key Takeaways
- Go multiple returns → TypeScript destructured tuples
- Variadic ...T → rest parameters ...T[]
- Go func literals → TypeScript arrow functions
- Both are first-class functions