Error handling in TypeScript. How to avoid exceptions.

  Рет қаралды 2,436

Beyond TypeScript

Beyond TypeScript

Күн бұрын

Пікірлер: 13
@podobayka
@podobayka Ай бұрын
I couldn't figure out the purpose of using neverthrow or the idea behind this approach. There was no information about it in the project repository either. Then I came across your video, and everything immediately became clear. Thank you!
@BeyondTypeScript
@BeyondTypeScript Ай бұрын
I'm glad it was helpful! The important thing it to understand the principle, the library you use is not that important. Heck, you can implement the Result type on your own too! ;)
@podobayka
@podobayka Ай бұрын
@@BeyondTypeScript Yea! You are totally right
@egorgor
@egorgor 7 ай бұрын
Thanks for the video, completely agree on the topic. Just wanted to point out that there is no need to use a library. It's enough to return a discriminated union type like Response = { type: 'error', reason: string } | { type: 'success', data: T }
@BeyondTypeScript
@BeyondTypeScript 7 ай бұрын
Completely agree!
@xorlop
@xorlop 3 ай бұрын
I like the idea of Either/Result types, because it clearly separates what is happy and sad path. However, I am curious your thoughts of just returning original value type and an Error? For example, safeParseInt would return int | Error vs Result. I have been littering code bases with this and it has proven useful, and I am actually not sure when it would be desired or wanted to return an Error type in the happy path, so I don’t see the benefit of separating the sad path out. Is there other benefits to wrapping in Result type I don’t see?
@BeyondTypeScript
@BeyondTypeScript 2 ай бұрын
Thank you for your comment! Generally speaking, for safeParseInt there are indeed just two outcomes - either int or Error (with only one possibility for sad path - 0). So, it's okay to return int | Error. However, the Result type help to turn a partial function (not defined for all inputs) into a total function (defined for all inputs), by returning a value of type Result instead of int (for example). Notice that the Result type takes two generics - Result and can be constructed in two ways - Ok or Err. However, the Error generic can hold another ADT for all possible outcomes (in more complex cases). For example, a file system operation can fail for a number of reasons - all of them can be defined as an ADT (by the way, same is true for the happy outcomes). This situation would be a bit cumbersome to describe without any differentiation mechanism - int | FileNotFound | BadPath | FileLocked | Whatever. Compare this to Result. On top of that, sometimes, there are cases where your function may return int | int - but which one is which? All in all, I'd say that it's a good practice to stick to a single code style - either throwing exceptions, or int | Error, or Result type. Mixing them will lead to confusion in the team. However, if I have a choice, I always use the Result type. I hope it all makes sense :)
@coder_one
@coder_one 11 ай бұрын
Have you had a chance to get acquainted with the "ts-belt" library? This is a library for TypeScript providing Option & Result types, but also other flavors from functional programming. And most interestingly, the source code of this library is written in Rescript! :)
@BeyondTypeScript
@BeyondTypeScript 11 ай бұрын
I didn't know about this library, thank you for sharing! But as I read ts-belt, the first thing I thought about was Rescript's Belt :) I'll give it a go.
@AndresJimenezS
@AndresJimenezS 3 күн бұрын
try a bit Effect ts ;)
@BeyondTypeScript
@BeyondTypeScript 2 күн бұрын
Oh, 100%. I've been keeping an eye on this for a long time.
@purovenezolano14
@purovenezolano14 2 ай бұрын
Recommend you switch to `neverthrow` as `ts-results` is unmaintained
@BeyondTypeScript
@BeyondTypeScript 2 ай бұрын
Thank you for the suggestion! Indeed, there are lots of alternatives to `ts-results`.
How Go Made Me A Better JS Dev (And Why You Should Learn It Too)
20:24
Doug's Coding Corner
Рет қаралды 11 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 12 МЛН
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 20 МЛН
This might fix error handling in JS
33:07
Theo - t3․gg
Рет қаралды 62 М.
Goodbye Exceptions! Hello Result Pattern!
9:16
Gui Ferreira
Рет қаралды 10 М.
JavaScript Error Handling: 5 Things You Aren’t Thinking About!
14:42
Options are Actually Great in TypeScript
14:53
Lucas Barake
Рет қаралды 1,2 М.
Exceptions vs error values in programming
12:11
Programming Duck
Рет қаралды 1,1 М.
Stop Recommending Clean Code
27:05
ThePrimeTime
Рет қаралды 540 М.
err != nil Is GOOD? (And Why)
7:19
ThePrimeTime
Рет қаралды 98 М.
5 Async + Await Error Handling Strategies
18:11
Wes Bos
Рет қаралды 23 М.
I'm Ditching Try/Catch for Good!
10:29
Web Dev Simplified
Рет қаралды 182 М.