---
title: "Discussion on \"Stop catching errors in TypeScript; Use the Either type to make your code predictable\" | Hashnode"
description: "Discussion on \"Stop catching errors in TypeScript; Use the Either type to make your code predictable\". In some languages such as Java, methods or functions can provide type information about the Exceptions or Errors they may throw. However in TypeScript, it is not possible to know what Errors a function may throw. In fact, a function could throw any v..."
url: "https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Stop catching errors in TypeScript; Use the Either type to make your code predictable](https://antman-does-software.com/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable?utm_source=hashnode&utm_medium=feed)

In some languages such as Java, methods or functions can provide type information about the Exceptions or Errors they may throw. However in TypeScript, it is not possible to know what Errors a function may throw. In fact, a function could throw any v...

Oct 23, 2021 · 7 min read · antman-does-software.com  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#functional-programming](https://hashnode.com/tag/functional-programming)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#javascript](https://hashnode.com/tag/javascript)

## Written by

[Anthony Manning-Franklin](https://hashnode.com/@Antman261)

@Antman261 · Perth, Australia

  
[View profile](https://hashnode.com/@Antman261)

Drunk on types and functional programming

Former CTO of SKUTOPIA I write about Functional Programming, TypeScript, Node, Event Sourcing, and engineering leadership

## Responses 7

Markdown supported · ⌘↵ to post

[VH](https://hashnode.com/@Silverbullet069)

[Apr 26, 2024](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/662b2b76b5f9fcb5364d25c2)

Very good design pattern to handle business logic exception. Yeah, those outcomes/exceptions/errors are from business logic, they are defined by developers. But what about, errors that are thrown from 3rd-party packages?

Example:

```
import {readSync} from 'fs'
...
const json = await readSync(pathToFile)
```

and somehow there is an exception throw from `readSync` via many reason (file not found, file corruption, some process delete it, ...). How will you handle this?

[Eirik Mo](https://hashnode.com/@EirikMo)

[Aug 21, 2023](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/64e3bacda9a8b9bbc8cf95f6)

This is great! I just tried it out for my signup flow and it makes my code so neat.

Would you know a good way to integrate this with Zod or the like?

[Jason Gill](https://hashnode.com/@jsongill)

[Jul 8, 2023](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/64a97af9395b682e51ab5b46)

Why use Either instead of a discriminated union?

[Toby Hobson](https://hashnode.com/@thobson)

[May 17, 2023](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/6464a31ca2c49bc61afe4329)

I just discovered this. Coming from a Scala background I really dislike the whole exception paradigm. Even Go (one of the most basic languages) shuns exceptions in favour of a primitive Either pattern.

Thanks so much for sharing!

[Julian](https://hashnode.com/@julianweb)

[Nov 5, 2021](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/6185448afd5d634d01698651)

Thanks for writing this! I agree, especially the first part about not (ab)using exceptions to model expected outcomes of a function/process.

Regarding the `Either<Left,Right>` approach. I'm not convinced. I can't find a compelling reason to prefer this over discriminated union types.

Perhaps you can think of an advantage?

Thanks again!

[ovo](https://hashnode.com/@oovvvv)

[Oct 26, 2021](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/6177ab2a0f452c1cd94fdd5a)

return;

[Jb Rocher](https://hashnode.com/@jibrocher)

[Oct 24, 2021](https://hashnode.com/posts/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable/61740f980f452c1cd94fa17c/comment/6175944c0c5e175a1c617c80)

Great Article, I consider myself to be somewhat experimented with Typescript, but I had never seen `Either` used this way. Thanks !
