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
Error Handling
MirrorLesson 5 of 10
Lesson 5

Error Handling

Handling failures: error values vs exceptions

Introduction

In this lesson, you'll learn about error handling 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 handling failures: error values vs exceptions.

TS
In TypeScript:

TypeScript has its own approach to handling failures: error values vs exceptions, 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
class NotFoundError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "NotFoundError";
  }
}

function findUser(id: number): User {
  if (id <= 0) throw new Error("invalid id: " + id);
  const user = db.get(id);
  if (!user) throw new NotFoundError("user not found");
  return user;
}

try {
  const user = findUser(42);
  console.log(user.name);
} catch (err) {
  if (err instanceof NotFoundError) {
    console.log("user not found");
  } else {
    console.log("error:", err);
  }
}

Comparing to Go

Here's how you might have written similar code in Go:

GO
Go (What you know)
import "errors"

var ErrNotFound = errors.New("not found")

func findUser(id int) (*User, error) {
    if id <= 0 {
        return nil, fmt.Errorf("invalid id: %d", id)
    }
    user, ok := db[id]
    if !ok {
        return nil, ErrNotFound
    }
    return user, nil
}

user, err := findUser(42)
if err != nil {
    if errors.Is(err, ErrNotFound) {
        fmt.Println("user not found")
    } else {
        fmt.Println("error:", err)
    }
    return
}
fmt.Println(user.Name)
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go returns errors as values; TypeScript throws exceptions

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go forces you to check errors at each call site; TypeScript can let them bubble up

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go's errors.Is for comparison; TypeScript's instanceof for error types

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go has no try/catch; TypeScript uses try/catch/finally

Step-by-Step Breakdown

1. Returning vs Throwing

Go functions return an error as the last value; TypeScript functions throw exceptions which unwind the call stack.

GO
Go
result, err := doSomething()
if err != nil { return err }
TS
TypeScript
try {
  const result = doSomething(); // may throw
} catch (err) {
  // handle it here
}

2. Custom Error Types

Go creates sentinel errors with errors.New; TypeScript extends the Error class.

GO
Go
var ErrNotFound = errors.New("not found")
TS
TypeScript
class NotFoundError extends Error {
  constructor(msg: string) { super(msg); this.name = "NotFoundError"; }
}

3. Error Propagation

In Go you explicitly wrap and return errors up the call stack. In TypeScript, uncaught exceptions propagate automatically.

GO
Go
return fmt.Errorf("processUser: %w", err)
TS
TypeScript
// Rethrow with context:
throw new Error("processUser: " + err.message);

Common Mistakes

When coming from Go, developers often make these mistakes:

  • Go returns errors as values; TypeScript throws exceptions
  • Go forces you to check errors at each call site; TypeScript can let them bubble up
  • Go's errors.Is for comparison; TypeScript's instanceof for error types
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 returns error values; TypeScript throws exceptions
  • Go: if err != nil — TypeScript: try/catch
  • Custom errors: Go sentinel errors → TypeScript extends Error
  • TypeScript errors bubble up automatically; Go requires explicit propagation
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