Hello everybody, and welcome to this talk about Zotter and TypeScript in your React application. So, I want to start with a story. So, almost a year ago, TypeScript was born. And every developer, JavaScript developer in the world probably was happy. So, we started to create interfaces to map our model. So, we start to use numbers, string, Boolean and so on. Or maybe types, depending on the scenario you can use interface or types. Whatever you prefer, it's not a problem.
And in our project everything was perfect. We had all the types perfect in our code base. And it was a perfect work for us. Until a day, a backend developer decided to change the API. And unfortunately, he or she decided to change the signature of the API. The result in our React application is something like this. Not a number, not a snippet string, or all the kind of strange JavaScript error that you can image in your React application. These are because yes, Tileset is fantastic. I love it. But after the transpilation, you lose all the types and the code becomes JavaScript code, because in your browser, run only JavaScript code and not tileset code. Sometimes developers forget this stuff and they don't realize this until in production, they don't receive this kind of error.
So, how we can solve this problem in our React or JavaScript or Tilescript applications? So, the superhero is Zod. What is Zod, first of all? Zod is a simple NPM package that you can install in your React application, in your project, using your Package Manager. You can use NPM, install, Zod, and everything is ready to use. Zod has a simple concept, the schema concept. The schema is an object, a real JavaScript object in this case, as you can notice, that describes your object. In this case, a customer schema is a Zod schema that describe that the customer is an object with four properties, the ID, with the type of number, the name with the type string, the email with the type string again, and the same for the phone. You can also add different constraints. For instance, the string can accept also a constraint that this is an email, the phone is a phone and so on. Or you can also build if you want your custom type. For instance, you want a specific case to describe, you can do this. But you can also, for instance, create a num, literal type, whatever you want.
Comments