Solving your “undefined” problem with well-placed errors (TypeScript Tip)

preview_player
Показать описание
Sometimes, TypeScript doesn't have the whole context for the system you're working within. When that happens, you can use this technique to align the types with the system as you know it to be.

Рекомендации по теме
Комментарии
Автор

With such great content and continuing for an year, your subscriber base is criminally low. Sadly, that's YouTube for us. Hope that your incentives are in check. Please don't retreat from asking for any monetary support should you need it. I'm sure lot of people will jump in.

ankitsinghjavac
Автор

I found your channel recently and until then I thought I knew Typescript pretty well. Now I can see how much more I can learn! Keep up with the quality content! I enjoy especially the typescript type challenges. When I'm doing them myself I often lack explanation why the solution I peeked works or doesn't and that's where your videos come in

krzysztofprzybylski
Автор

Congrats on 1 year. Many larger channels I see reference your awesome Typescript content. I've seen you show up in Theo's and Primeagen's videos. The community is definitely taking notice of your good work! 🙏

kettenbach
Автор

Keep up the awesome quality! Genuinely one of the few channels where I watch every single upload !

mmmike
Автор

You’re an underatted TS guru. I love the topics you pick, it’s always relevant to the issues I try to solve on my own projects.

johnyepthomi
Автор

Your TypeScript series are just great. Thank you.

can
Автор

you HAVE TO post more Typescript videos, I’m learning so much from them

ikhito
Автор

Great way to solve it. Thanks for sharing the idea!
If I were to code-review, I’d however suggest that the error case should have a check against undefined and not falsy, otherwise you may run into a case where the correctly found element is falsy and it raises an error.

Keep up the great work, I think you deserve so much more attention for the quality you’re delivering!

macchicken
Автор

I like returning a union of the type and an Error instead of throwing errors. This does force you to handle the error in the calling function, but there's no way around that, Your find function might not return a value and this is because it relies on an external data source. In many cases, this allows you to handle this error in your application in a graceful way instead of the application crashing because it threw and error. Also throwing an error requires you to use try catch which takes you out of the normal control flow. By making it a union of the return type and an error, you can handle it with an if statement or switch statement, and the code is much easier to read and reason about. (and yes, I'm inspired by rust's Result type here in this case)

echobucket
Автор

Keep on doing those videos, We support you 😊

artyomdanilov
Автор

Great video, I much prefer throwing an error over casting or using the non null assertion. My general perspective on typescript is that I'll always choose an approach without casting if it's possible

thematrix
Автор

In other words - when we know that an operation should not be ambiguous, any ambiguity should result in an error. In this way we are forcing any unexpected behavior to manifest itself as an exception, while creating the context for TS at the same time. Nice.

i.j.
Автор

This is actually incredible. I've solved this problem in other ways before, but this is both clean and pretty simple

dinckelman
Автор

This awesome moment of my life where I am able to watch a video about TypeScript because someone took the time to create a video about TypeScript and I must absolutely watch it so there I am 😅😅😅😅

Luxcium
Автор

Cool. The first approach is pretty clear. I just always use like "as Member" to omit "undefined" :)
Thanks!

vphtfgu
Автор

Nice! I like the second solution with the getter function. I will be trying that in my codebases as I often have this type error.

adamdrake
Автор

1.Global Positioning System
2.Undefined
3.Kalkulator
4.Rudi Harmanto

SamsungAe-dpkj
Автор

it is just so interesting to watch your videos, i like your narration style comparing to other YouTubers, you just feel natural and expressive. You are the best to be honest, better than the wizard :) . Please keep posting these interesting videos About TypeScript.

chakir.lilallah
Автор

Love your videos. Especially practical ones like this one! Thanks for sharing your knowledge!

Where do you work?

DedicatedManagers
Автор

Why not just use an exclamation mark, if the member is undefined actually undefined the same runtime error will occur

plusquare