Use ‘unknown’ instead of ‘any’ in TypeScript (Try / Catch error handling)

  Рет қаралды 28,987

ByteGrad

ByteGrad

Күн бұрын

Пікірлер: 40
@ByteGrad
@ByteGrad 8 ай бұрын
👉 NEW React & Next.js Course: bytegrad.com/courses/professional-react-nextjs
@ismoiljonabduqahhorov5825
@ismoiljonabduqahhorov5825 Жыл бұрын
Hello ByteGrad I was struggling with error handling and your tutorial really helped dealing with errors loved your content good luck
@renjithpn2002
@renjithpn2002 7 ай бұрын
This is art, loved it 🙂
@elenamikhaylova5086
@elenamikhaylova5086 8 ай бұрын
Thank you for explaining this well, i have banged my head on JS error handling for so long
@vincenzocristiano135
@vincenzocristiano135 Жыл бұрын
This is pure gold! Thanks a lot!!!
@honkhonkv2236
@honkhonkv2236 10 ай бұрын
Hello, awesome channel for a noob like me! I was wondering, at 10:42 in our utility function couldn't we just omit to type the message as string and instead in the return do something like "return message.toString()" ?
@MailarYT
@MailarYT Жыл бұрын
Your really good explaining, keep going Cheers
@loydcose2780
@loydcose2780 Жыл бұрын
This is awesome!
@solosdev6946
@solosdev6946 9 ай бұрын
Thanks finding a solution for this :D
@grugbrain
@grugbrain Жыл бұрын
That's exactly how I handle request errors in our projects. 😅 Those Python develelopers cannot agree with Java developers on error response, and they both think it is easy to handle those errors in the client side.
@Bravin_Joshua
@Bravin_Joshua 2 ай бұрын
Nice lesson on error handling though
@СергійК-ш4у
@СергійК-ш4у Жыл бұрын
thank you for sharing this topic
@npaul1440
@npaul1440 Жыл бұрын
Your tutorials and content are A Class. Just get a better microphone if possible. Thanks
@mujibulhaquetanim
@mujibulhaquetanim Жыл бұрын
yap, sounds is too low.
@bheemkumarthami
@bheemkumarthami 11 ай бұрын
Thanks a lot. keep going
@meka4996
@meka4996 Жыл бұрын
Amazing! Thanks
@SyTruong-d3d
@SyTruong-d3d Жыл бұрын
We still not consider the case where "message" is also an object
@antonapalanyuk338
@antonapalanyuk338 Жыл бұрын
What about to save mark as 'any' and use following: console.log(error?.message || error?.stack || error); It is suitable for all error data types
@andrevinsky
@andrevinsky 2 ай бұрын
Wrapping an unknown in a String() call does not actually constitute casting, strictly speaking, as it’s either this or this: as string. Wrapping stuff in a String() call actually replaces the value of the unknown with a string equivalent. Not casting That note aside, I really like videos. Appreciate the effort
@Axorax
@Axorax 8 ай бұрын
I didn't know there was an unknown type 😮
@scide-x7242
@scide-x7242 Жыл бұрын
Thank you chris
@lucio9601
@lucio9601 2 ай бұрын
God, I hate TS. Yet its so needed.
@namstel9225
@namstel9225 Жыл бұрын
I prefer typeof error?.message === "string"
@mryo_
@mryo_ Жыл бұрын
Thanks. export const getErrorMessage = (error: unknown) => { if (error instanceof Error || (error && typeof error === "object" && "message" in error && typeof error.message === "string")) { return error.message; } else if (typeof error === "string") { return error; } return "Something went wrong"; };
@charuwaka1
@charuwaka1 11 ай бұрын
use a generic API response wrapper
@ajaychandel9976
@ajaychandel9976 Жыл бұрын
cool
@virtual5754
@virtual5754 Жыл бұрын
const res = ... as unknown as any
@AFE-GmdG
@AFE-GmdG Жыл бұрын
I would write ... else if (error && typeof error === "object" && "message in error && typeof error.message === "string") { ... If there is a message in an error object but it isn't a string, it should possibly use the default error message instead.
@ByteGrad
@ByteGrad Жыл бұрын
Good point
@AriadyPutra
@AriadyPutra 7 ай бұрын
message = `${error}`
@supremecommanderz
@supremecommanderz 7 ай бұрын
just type and be happy: error?.message??'errors happens'
@deatho0ne587
@deatho0ne587 Жыл бұрын
What if error.message is is an object, HTML, etc... Casting to string then would not be really safe.
@ByteGrad
@ByteGrad Жыл бұрын
Good point
@deatho0ne587
@deatho0ne587 Жыл бұрын
It is good for another video about TypeGaurds maybe.
@ciach0_
@ciach0_ Жыл бұрын
Then that would be [object Object]
@deatho0ne587
@deatho0ne587 Жыл бұрын
The thing is even if it is an object should you trust it for display purposes?
@charuwaka1
@charuwaka1 11 ай бұрын
use zod
@silenthooman
@silenthooman Жыл бұрын
the fn is async, and he used the then, man just use await
TypeScript Generics are EASY once you know this
22:21
ByteGrad
Рет қаралды 149 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
This might fix error handling in JS
33:07
Theo - t3․gg
Рет қаралды 64 М.
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 130 М.
Error handling in TypeScript. How to avoid exceptions.
6:11
Beyond TypeScript
Рет қаралды 4,8 М.
JavaScript Error Handling: 5 Things You Aren’t Thinking About!
14:42
Why I don't use React-Query and tRPC in Next.js
18:58
ByteGrad
Рет қаралды 104 М.
JavaScript Error handling in 9 minutes! ⚠
9:03
Bro Code
Рет қаралды 21 М.
I'm Ditching Try/Catch for Good!
10:29
Web Dev Simplified
Рет қаралды 198 М.
any vs unknown vs never: TypeScript demystified
8:01
Andrew Burgess
Рет қаралды 26 М.
Typescript Unknown vs Typescript Any - What Is the Difference?
10:48
Monsterlessons Academy
Рет қаралды 6 М.
Stop using div in React (+ Fragment, Semantic Tags)
15:48
ByteGrad
Рет қаралды 117 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН