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

Поделиться
HTML-код
  • Опубликовано: 29 сен 2024

Комментарии • 37

  • @ByteGrad
    @ByteGrad  4 месяца назад +1

    👉 NEW React & Next.js Course: bytegrad.com/courses/professional-react-nextjs

  • @npaul1440
    @npaul1440 10 месяцев назад +8

    Your tutorials and content are A Class. Just get a better microphone if possible. Thanks

  • @charuwaka1
    @charuwaka1 6 месяцев назад

    use a generic API response wrapper

  • @supremecommanderz
    @supremecommanderz 3 месяца назад

    just type and be happy:
    error?.message??'errors happens'

  • @renjithpn2002
    @renjithpn2002 3 месяца назад +2

    This is art, loved it 🙂

  • @honkhonkv2236
    @honkhonkv2236 6 месяцев назад +1

    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()" ?

  • @mryo_
    @mryo_ 10 месяцев назад +1

    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";
    };

  • @ismoiljonabduqahhorov5825
    @ismoiljonabduqahhorov5825 Год назад +5

    Hello ByteGrad I was struggling with error handling and your tutorial really helped dealing with errors loved your content good luck

  • @SyTruong-d3d
    @SyTruong-d3d 11 месяцев назад +3

    We still not consider the case where "message" is also an object

  • @MailarYT
    @MailarYT Год назад +5

    Your really good explaining, keep going Cheers

  • @Axorax
    @Axorax 3 месяца назад

    I didn't know there was an unknown type 😮

  • @namstel9225
    @namstel9225 11 месяцев назад +1

    I prefer
    typeof error?.message === "string"

  • @elenamikhaylova5086
    @elenamikhaylova5086 4 месяца назад +1

    Thank you for explaining this well, i have banged my head on JS error handling for so long

  • @grugbrain
    @grugbrain Год назад +2

    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.

  • @AriadyPutra
    @AriadyPutra 3 месяца назад

    message = `${error}`

  • @solosdev6946
    @solosdev6946 5 месяцев назад +1

    Thanks finding a solution for this :D

  • @charuwaka1
    @charuwaka1 6 месяцев назад

    use zod

  • @ajaychandel9976
    @ajaychandel9976 7 месяцев назад +1

    cool

  • @СергійК-ш4у
    @СергійК-ш4у 10 месяцев назад +1

    thank you for sharing this topic

  • @loydcose2780
    @loydcose2780 8 месяцев назад +1

    This is awesome!

  • @vincenzocristiano135
    @vincenzocristiano135 11 месяцев назад +1

    This is pure gold! Thanks a lot!!!

  • @meka4996
    @meka4996 Год назад +1

    Amazing! Thanks

  • @antonapalanyuk338
    @antonapalanyuk338 11 месяцев назад

    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

  • @AFE-GmdG
    @AFE-GmdG Год назад +5

    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.

  • @bheemkumarthami
    @bheemkumarthami 7 месяцев назад

    Thanks a lot. keep going

  • @АлександрГерасимов-с3щ
    @АлександрГерасимов-с3щ 11 месяцев назад

    const res = ... as unknown as any

  • @scide-x7242
    @scide-x7242 Год назад

    Thank you chris

  • @deatho0ne587
    @deatho0ne587 Год назад +2

    What if error.message is is an object, HTML, etc... Casting to string then would not be really safe.

    • @ByteGrad
      @ByteGrad  Год назад +2

      Good point

    • @deatho0ne587
      @deatho0ne587 Год назад +1

      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?

  • @silenthooman
    @silenthooman 11 месяцев назад

    the fn is async, and he used the then, man just use await