I've been trying to understand the distinctions between using types, enums, and const in TypeScript. Could you shed some light on how they differ and when to use each one?
Types is a way to define what kind of values does the variable hold and it helps with type safety, catching issues while developing etc. For example `const status: string = 'active'` Enums are a way to define a set of named constants. For example when we have a variable of status and we know the status can be Active or Inactive only, that's when enums are most useful. Then these enums can be used to validate the input or whenever we need to use the status we can see which values it can be. Const is just a way to define a constant variable, same as in javascript. Constant means the value of the variable never changes. If the type is an array then we can mutate it like add/remove items but if we define a string we can't reassign it. Hope that helps.
The reason why I’m checking the types there is because in my types they are set as required properties, but because they are input from the user we can’t be sure the user has actually sent all the required fields. I could’ve made the properties optional and then typescript will complain if they are not checked.
Can you imagine having a large object with different fields being returned. Zod is heaven sent.
100%! I don’t use it enough, I use AJV more, but I will be using it for my new side project.
I just made the decision to use Zod inside Typescript projects after I read some really interesting articles on the net.
Let us know how you're finding it after some time :)) @@heavierthanlight7173
@@LjupcheVasilev will do!
Really great rundown of useful tips for TypeScript! Thanks for the good work!
Glad it was helpful!
Will you make course on typescript or Vuejs or Nuxtjs or NodeJs ExpressJs or Nestjs or Graphql?
Probably not a whole course soon. But I will continue to make videos on those subjects.
I've been trying to understand the distinctions between using types, enums, and const in TypeScript. Could you shed some light on how they differ and when to use each one?
Types is a way to define what kind of values does the variable hold and it helps with type safety, catching issues while developing etc. For example `const status: string = 'active'`
Enums are a way to define a set of named constants. For example when we have a variable of status and we know the status can be Active or Inactive only, that's when enums are most useful. Then these enums can be used to validate the input or whenever we need to use the status we can see which values it can be.
Const is just a way to define a constant variable, same as in javascript. Constant means the value of the variable never changes. If the type is an array then we can mutate it like add/remove items but if we define a string we can't reassign it.
Hope that helps.
Very descriptive and informational thank you :)
You're very welcome!
Wow this channel is so good 👍 👍 👍
Thank you so much 😀
I think you don't need to check their types here 2:41 you already have defined types of it so TS will throw an error and you don't have to check it
The reason why I’m checking the types there is because in my types they are set as required properties, but because they are input from the user we can’t be sure the user has actually sent all the required fields.
I could’ve made the properties optional and then typescript will complain if they are not checked.
Great video man, keep going
Thank you!
That was great , You can deliver much better content , Keep it up
Thank you, glad you like it :)