CUSTOM ERRORS in TypeScript? - Advanced TypeScript

preview_player
Показать описание
Become a TypeScript Wizard with Matt's upcoming TypeScript Course:

Follow Matt on Twitter

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

you can do this even better by separating the error message as a literal
like
const errorMessage = "You can not..." as const
so you dont have to update two error message strings later.

nomadshiba
Автор

As usual Matt, these hints are amazing! This one’s a beauty - thank you :)

SimonWinter-nz
Автор

Why is the function called deepEqualCompare if it doesn't compare deeply?

loic.bertrand
Автор

0:30, actually, === compares whether two variables are the same reference in memory and that's why it doesn't work. Arrays (and Functions too) are objects and objects are passed by reference, everything else (primitives) is passed by value. Since those two arrays have the same data but different memory addresses it returns false. 😁

igorskyflyer
Автор

These tips are so useful, thank you for posting them. I can't wait for the course.

kurishutofu
Автор

So cool! Was wondering how you pulled this off in Xstate

kevinwuwon
Автор

Hey Matt, typescript enthusiast here,
I wanted to ask if this is more of a Cheese than a solution to type level error checking.

If I understand correctly, your entire string "You cannot compare two arrays using deepEqualCompare" is a literal type, its not an error message. When we observe compiler errors with intellisense, the messages are coming from interfaced objects "message" property that are implemented (pretty sure?), not literal types. This is why when you read your custom error messages they're wrapped in quotes, versus the builtin type errors that tell you not to assign strings to incompatible types etc.

Seeing your type literal pop up in the error message prompt was like a "wow, that works" moment lmfao, but is it right?

Kwuasimoto
Автор

I propose a challenge, because I at least can't think of how to do it. But how could I in Typescript indicate that a function or method can throw an exception? Forcing to use a try-catch. I guess it's not possible, I didn't find anything

bobobo
Автор

you had 69 likes in this video, sorry for making it 70 <3

sumdavias
Автор

I thought you could do `type CheckForBadArgs<Arg> = Arg extends Primitive ? Arg : 'Has to be a primitive for checking'` but it seems Primitive is d3 type and not from TS libs... Which is a shame.

ColinRichardson
Автор

Hey there sorry for dropping this here but have you ever made bots for games?

dome
join shbcf.ru