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

  Рет қаралды 23,364

ByteGrad

ByteGrad

Күн бұрын

👉 NEW React & Next.js Course: bytegrad.com/courses/professi...
Hi, I'm Wesley. I'm a brand ambassador for Kinde (paid sponsorship).
👉 Add authentication to your app FAST: bit.ly/3QOe1Bh
👉 NEW React & Next.js Course: bytegrad.com/courses/professi...
👉 Professional JavaScript Course: bytegrad.com/courses/professi...
👉 Professional CSS Course: bytegrad.com/courses/professi...
👉 Discord: all my courses have a private Discord where I actively participate
🔔 Email newsletter (BIG update soon): email.bytegrad.com
⏱️ Timestamps:
0:00 data: unknown
1:06 ts-reset
1:36 error: unknown
4:59 Handle unknown
8:03 Utility function
#typescript #typescripttutorial

Пікірлер: 37
@ByteGrad
@ByteGrad 29 күн бұрын
👉 NEW React & Next.js Course: bytegrad.com/courses/professional-react-nextjs
@renjithpn2002
@renjithpn2002 Күн бұрын
This is art, loved it 🙂
@vincenzocristiano135
@vincenzocristiano135 8 ай бұрын
This is pure gold! Thanks a lot!!!
@elenamikhaylova5086
@elenamikhaylova5086 Ай бұрын
Thank you for explaining this well, i have banged my head on JS error handling for so long
@ismoiljonabduqahhorov5825
@ismoiljonabduqahhorov5825 10 ай бұрын
Hello ByteGrad I was struggling with error handling and your tutorial really helped dealing with errors loved your content good luck
@MailarYT
@MailarYT 11 ай бұрын
Your really good explaining, keep going Cheers
@user-vb4vs5yz6w
@user-vb4vs5yz6w 7 ай бұрын
thank you for sharing this topic
@meka4996
@meka4996 9 ай бұрын
Amazing! Thanks
@npaul1440
@npaul1440 7 ай бұрын
Your tutorials and content are A Class. Just get a better microphone if possible. Thanks
@mujibulhaquetanim
@mujibulhaquetanim 5 ай бұрын
yap, sounds is too low.
@loydcose2780
@loydcose2780 5 ай бұрын
This is awesome!
@honkhonkv2236
@honkhonkv2236 3 ай бұрын
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()" ?
@bheemkumarthami
@bheemkumarthami 4 ай бұрын
Thanks a lot. keep going
@solosdev6946
@solosdev6946 2 ай бұрын
Thanks finding a solution for this :D
@scide-x7242
@scide-x7242 10 ай бұрын
Thank you chris
@WilliamShrek
@WilliamShrek 9 ай бұрын
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.
@user-zj9jd8hd7q
@user-zj9jd8hd7q 8 ай бұрын
We still not consider the case where "message" is also an object
@antonapalanyuk338
@antonapalanyuk338 8 ай бұрын
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
@Axorax
@Axorax 24 күн бұрын
I didn't know there was an unknown type 😮
@ajaychandel9976
@ajaychandel9976 4 ай бұрын
cool
@AFE-GmdG
@AFE-GmdG 10 ай бұрын
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 10 ай бұрын
Good point
@mryo_
@mryo_ 6 ай бұрын
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 3 ай бұрын
use a generic API response wrapper
@panbotuk
@panbotuk 8 ай бұрын
Why use unknown and any in typescript?
@user-ik7rp8qz5g
@user-ik7rp8qz5g 8 ай бұрын
const res = ... as unknown as any
@namstel9225
@namstel9225 8 ай бұрын
I prefer typeof error?.message === "string"
@deatho0ne587
@deatho0ne587 10 ай бұрын
What if error.message is is an object, HTML, etc... Casting to string then would not be really safe.
@ByteGrad
@ByteGrad 10 ай бұрын
Good point
@deatho0ne587
@deatho0ne587 10 ай бұрын
It is good for another video about TypeGaurds maybe.
@ciach0_
@ciach0_ 10 ай бұрын
Then that would be [object Object]
@deatho0ne587
@deatho0ne587 10 ай бұрын
The thing is even if it is an object should you trust it for display purposes?
@AriadyPutra
@AriadyPutra 4 күн бұрын
message = `${error}`
@charuwaka1
@charuwaka1 3 ай бұрын
use zod
@silenthooman
@silenthooman 8 ай бұрын
the fn is async, and he used the then, man just use await
Why use Type and not Interface in TypeScript
14:12
ByteGrad
Рет қаралды 193 М.
Why You Should Always Help Others ❤️
00:40
Alan Chikin Chow
Рет қаралды 136 МЛН
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 47 МЛН
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 66 МЛН
The BEST way to host Next.js websites
17:37
ByteGrad
Рет қаралды 24 М.
Vite React to GitHub Pages: Fix Empty Page & Missing Assets
4:14
Vladyslav Dihtiarenko
Рет қаралды 376
🔥 TypeScript's `never` type explained
1:36
Wes Bos
Рет қаралды 8 М.
TypeScript Generics are EASY once you know this
22:21
ByteGrad
Рет қаралды 125 М.
Microsoft's First Phone... From 1998
18:12
Michael MJD
Рет қаралды 7 М.
TypeScript UNKNOWN and ANY types - Advanced TypeScript
4:35
cn() - Every Tailwind Coder Needs It (clsx + twMerge)
7:46
ByteGrad
Рет қаралды 116 М.
Why You Should Always Help Others ❤️
00:40
Alan Chikin Chow
Рет қаралды 136 МЛН