GO
TS

Go to TypeScript

10 lessons

Progress0%
1Variables & Types2Functions3Structs & Classes4Interfaces — Both Use Structural Typing5Error Handling6Goroutines vs Async/Await7Slices & Maps → Arrays & Objects8Packages & Modules9Testing10Generics
All Mirror Courses
GO
TS
Functions
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
GO
From Go:

In Go, you're familiar with defining and calling functions with types.

TS
In TypeScript:

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:

TS
TypeScript 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:

GO
Go (What you know)
// 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 }
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go supports true multiple return values; TypeScript returns tuples (arrays)

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go uses variadic ...T; TypeScript uses rest parameters ...T[]

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

TypeScript arrow functions are more concise than Go's func literals

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

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.

GO
Go
func add(a, b int) int
TS
TypeScript
function add(a: number, b: number): number

2. Multiple Returns

Go natively supports multiple return values; TypeScript emulates this with destructured arrays.

GO
Go
func minMax(arr []int) (int, int) {
    return arr[0], arr[len(arr)-1]
}
min, max := minMax(nums)
TS
TypeScript
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.

GO
Go
transform := func(x int) int { return x * 2 }
TS
TypeScript
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
Common Pitfall
Don't assume TypeScript works exactly like Go. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go multiple returns → TypeScript destructured tuples
  • Variadic ...T → rest parameters ...T[]
  • Go func literals → TypeScript arrow functions
  • Both are first-class functions
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Go code in TypeScript to practice these concepts.
PreviousNext