How to use TypeScript to hire employees in a web group How to use TypeScript in a VSC project How to alors create .sush I'd like to show you some a little bit advanced concepts from TypeScript just to clarify some things and maybe make you more comfortable using these types and see the value in them.
Today I'm here with Andrey from Content.ai. He'll help me manage the questions and chat. So if you've got any questions, feel free to ask. And ok, yep, that's me, you can contact me for example on GitHub you can see some things I work on. at the at content by a company profiling it up as well and okay I was gonna be on the other agenda we're gonna start by discussing some advantages of using the strict strict types like the strictest times that are available for the situation.
Next we'll work on the first example where there will be a small bug in an application that I've prepared and we'll talk about how better typing or better use of the Typekit type system would prevent such a bug and during that we'll uh we'll try out some of the some of the typeset concepts then we'll go on to another example and then depending on the time and maybe our energy we we can try the bonus that the bonus stuff that i've prepared that's a little bit more advanced more about playing with typescript and stuff like that so we'll see if if you are interested in that so at first uh i'll show you the application that we'll be working on it's just a simple events application that manages some events you've got you've got events maybe just a little bit i think i've got the buggy version right now running So, I'll run the other one.
Yeah, the application itself. You can maybe just to stop that you can download it from this URL. I'll copy paste it into the chat so that you don't have to find it look for it in the on the screen it's all the other how great thanks so thank you for that and now the application should run smoothly and as I said it's a very simple application that manages some events, you can click on the event, there's a description, their names, I can create a new event, I can select an organizer and it's there. I can delete events, I can create events for a certain organizer right from here, and I can copy events. So very simple. And yeah. So let's continue with this.
So, why would we even want to bother with with types and type system. The thing is, I often see that people think about types as their enemies, something they have to combat with and there are always obstacles in their way. But to do this, type system is there to help you. It can find a, it can catch bugs. It can really catch a lot of bugs And it's there to have your back in case you do some typos and mistakes, this is very often. And can also help you explore APIs. Like, often if I start with a new library, I just type some things, some function names and stuff like that, and I look for types, what the IntelliSense, for example, offers me, in my code editor.
For example, if I open this example file, you can see a function, write by reference that accepts an object, a reference, that has two optional properties, ID and external ID. And now from this API, I say okay, I'm going to call the function, and what should I provide the function? Well, I have no idea. Should I call it with an empty object? TypeSketch is fine with that. The TypeSketch is not able to advise me because according to TypeSketch, this is fine. But it probably isn't fine because the reference is just an empty object. There's nothing. Probably what the author really intended to do is to say, okay I want you to provide either an ID or an external ID. That's probably what the author meant but he didn't provide me any exact guidelines for that. So I'm lost here. But if he instead did something like this like this, okay, I want a union type where I want the reference to be an object with one property id that has to be there, or another, or a a different object that instead has a property external ID, that's not optional and always has to be there, now, the type key tells me, okay. MT object is not a valid parameter for this function. You either need to provide an object with ID property or external ID property, but either one has to be there. And at the same time, if I say... This is fine. That's disappointing. I thought he was going to tell me that I can't provide both of them, but well, that's not the case. But at least I know I have to provide one of them. So in this way, TypeScript really helps me to explore the API and see what's available and what I have to or don't have to provide. It's really much more clear even if you read the type, you see one of them is available. Or one of them is necessary. uh regarding the preciseness of types uh you can you can also often see a function like this a function takes some string that's uh that uh has to repre that represents some kind of action you want to take for in this example uh you want you want to create an event of a certain type and then you switch on the type and you provide different kind of event for each type. And then if you receive an invalid, some kind of unknown event type, you just throw an error because that's an invalid state for you, right? And then as a user of such an API, I'm going to go and say, okay, I'm going to call this event type and I have to provide a string. Okay, what kind of string? I don't know. I'm just going to slap there some string. and it's going to throw an error because this is neither of these strings that are used by the function. Again, if the author of the API would instead write something like this... and now, I've got an error. and the typeskip tells me, okay, you have to provide this or that. Pretty clear.
Comments