Hello, everybody. My name is Ethan Erewood. I'm a software engineer for Microsoft and today I'm going to be talking to you all about safely handling dynamic data with TypeScript.
So, handling data. What is data? As software developers, we use a lot of it and in a lot of different ways. Some good examples, at least of how I use it is within API routes, when building a back end service, dealing with forms in the front end, and also authentication payloads and all of the surrounding things that go with authentication in an entire full stack application. And this is just a short list. You can only imagine how long this can get when you start dealing with databases or data science and just general, any sort of communication between large systems.
So, let's take a peek at an example of a record of data. In this case, I'm using a JSON object. We got a bunch of keys here. ID, name, employed, company, age, and projects. We're representing a person. Maybe this is an employee directory or maybe it's a user directory for a site such as LinkedIn where we have a user, we want to get their name, we want to know are they employed or not, which is a Boolean value. We want to know what company they work for. We want to know how old they are. And we also might want to list their projects. And as many folks know, JSON is a very verbose way of representing data. There are it has a lot of great primitives that are all based in JavaScript, and it can be quite extensive. In fact, entire APIs are powered by just JSON projects alone through the open schema format.
Talking about backend APIs, let's take a look at a Fastify route. In this case, we're defining a post route. The path is add user, and the request handler here has two arguments, request and response, and we're destructuring the body from that request object. Does anyone know what type body might be? Is it a record, an object, is it any type? Trick question. It's unknown. The body property of that request object, taking a peek again at the code, the Fastify route has no idea what it is because in context of Fastify as a framework, we're not sure what the developer intends to be coming in through their request. And there's no way for Fastify to know that when you're right when the code is being written or even compiled. Well, maybe not when it's compiled. We'll get to that later. So let's take another look here.
Comments