Skip to main content
View on YouTube to comment and like!

Syntax Video

TypeScript Utilities You Should Know

Chapter 1 0:00

Intro

have you ever been working with some library that uses typescript and you need to get access to some type that's like returned by one of their built-in functions or maybe there's a built-in type that's actually only accessible inside of some other type in this video I'm going to show you a bunch of built-in helpers in typescript that will let you do just that so let's get into it my name is CJ welcome to syntax and for this example I just have a Prisma query so I'm querying a restaurants table and including all of the related items like menu items and the city and the orders and each of those I'm potentially also including fields too and also like the restaurant owner and like uh all of their nested properties and different things like that so you could imagine that the return type of this function is going to be very complex now I'll mention Prisma does have built-in type helpers to get the types of the return of this function here uh but I'm just using this as an example because you might be dealing with a library that doesn't have those built-in type helpers and at the end of the video I'll show you how we can use

Chapter 2 0:25

A Complex Type

the bu in Prisma but the first thing is I want to get the type of this return function right it's some super complex nested type how do I do that built into typescript is the return type helper so I can create a type called restaurant and set it equal to return type and then pass in the type of that function so if I say type of get restaurant I now have a type that represents that return type so for instance I could have a function that's like format response or something maybe I got this restaurant from an API maybe I'm not dealing with this directly next to the database call I could have like the restaurant here pass that in and then potentially use it so let's just log restaurant Dot and we'll notice our first first issue so you can see here that this has like catch and then on it and that's because this actually returns a promise so your next helper is the awaited helper essentially if I have a type that represents an awaited value or basically a promise I can use awaited and that will give me the value inside of it so I can do it like this and now restaurant just represents the

Chapter 3 1:00

ReturnType

Chapter 4 1:34

Awaited

restaurant type itself not a promise so you'll notice if I do dot now we get access to the actual properties because we've essentially extracted the return type and and got rid of all the promise and everything and we have a a good old restaurant type that we can now use now your next helper deals with getting the keys of a specific type so let's say I had a function that's called like validate field and it takes in a specific key here we'll say that it's a string and we're also going to pass in a restaurant instance now uh first of all let's just try logging the restaurant at that specific key now if this were JavaScript we could do this right I mean technically we still could do this in typescript if we get if we ignored it but typescript is telling us hey that key could be anything um it it's not just limited to the key specifically on restaurant so you probably already know about this one but I just want to show it to you anyways let's say we call this the restaurant Fields I can simply just say key of restaurant and now I have a union type that represents all of the fields on that specific restaurant however you can see that it says never because I forgot one thing so this uh

Chapter 5 2:09

keyof

Chapter 6 2:51

NonNullable

database call could actually return null so if we look at this it's this whole value or null so we need one more helper I'm actually going to use uh non nullable so non-nullable it's built into typescript and if I pass this type into that now I have a type that cannot be null you can see that it removes that that null property and now whenever we say key of we can see that this is a type Union that has all of those specific fields and now this is useful here because I can pass in restaurant Fields and now typescript is happy because if I call this function elsewhere right if I say validate field and I pass in let's say like City uh typescript is going to be just fine with that and of course I have to pass in a valid restaurant type um but if I were to pass in banana typescript is going to complain because hey that's not a valid key on that specific type now it's important to note that this is still just typescript we're not doing any sort of runtime validation we're basically defining these types and then writing our code as if we had those types already in that form right here I'm basically expecting that restaurant is going to be in this shape whenever I it gets passed into this function if I

Chapter 7 3:39

NOT Validation

wanted any actual runtime validation I'd need a library like that for like Zod or Sinclair type boox or I could write it by hand but it's important to not I'm not actually validating these types I'm just writing code that is very editor friendly and I'm basically telling typescript what I'm going to expect and in turn it's going to show me errors if the code I'm writing does not adhere to the types that I have that I'm using or that I've defined here but again this is not runtime validation this is just making it so that our code is is a bit nicer and uh giving us errors in the way that we want according to the types that we've defined now your next helper is nothing crazy but essentially what if I want to type of some nested type so if you look at restaurant it has a bunch of things it has like menu items and orders and City and owner let's say I want a type that represents City that's inside of that type I can just use bracket notation so let's say city is restaurant bracket you can see I get access to all these fields and if I say City I now have a type that represents just that City type so I could write a function that's like geolocate and it accepts a city type and I could use that type there now your next helper is very useful if you're dealing with like API responses where the properties are

Chapter 8 4:26

Nested Types

Chapter 9 4:51

Partial

potentially undefined or something like that and it's called partial so let's say I have a type here we're to call it bad restaurant and I can say partial and then pass in my type and now I have a type where every single property is optional and so like I said it's super useful if you're dealing with like an API response right so if I have a function that's like a format response and it takes in a restaurant but this is going to be a bad restaurant now I'm telling typescript that my code should have lots of guard checks right because if I try to access specific properties on that restaurant let's like let's say city. name then I need to have some check to make sure that is not undefined in this case we're using optional chaining I could use an if statement whatever else but I'm basically telling typescript hey make sure that I do the work down here because this value could potentially be missing some of those fields now another useful built-in type is actually the reverse of this like let's say you have some type that has a bunch of optional properties but you know that the data you're going to be dealing with as those properties you can use required so let's create a type called Good restaurant and this is required and then we'll pass in the bad

Chapter 10 5:40

Required

restaurant so we went we went the other way we took we took a type that had a bunch of optional properties and then created a type or every single property is now required and again this is useful if let's say you've already done some validation and you know that those properties can't be undefined anymore because you have some code above it that validated them you could use this type now and not have to do this optional chaining everywhere if you're sure that that type isn't going to have optional properties anymore now your last two type helpers allow you to take a type and specifically choose the properties you want on it to to create a new type or get rid of some properties and so the first one is called pick and we can pick specific properties that we want so let's call this restaurant minimal and it's going to be to pick we'll pass in the type restaurant and then we can pass in a type Union of keys and so let's say I want to just have the ID type on this object here and now if we look at it we'll see it's an object that will only have an ID but this can be a type Union so I could also pass in any valid key like name and then let's also pass in like orders and now I have a type that only has those three properties now again this is super useful if you know that you're only dealing with like a subset of the properties of some type

Chapter 11 6:16

Pick

and you don't want the type to lie about all those extra properties you can just use pick now the reverse of this is to take a type and just omit a few properties and that helper is called omit so if we call this restaurant mostly we can say omit pass in the type and here we'll say we want to Omit like the orders like this and so now I have a type where it has all the properties except for orders but let's also not include like menu item and now if we look at the type it doesn't have that specific property so that's it for all of the type helpers that I'm going to show you here I highly recommend you check out utility types in the typescript documentation cuz it has quite a few that I didn't mention here but all of these are super useful if you're working with some library and you need to perform some modifications on some of the types on on that specific library that you're dealing with now I did say I would show you how to do this with Prisma itself so if you look in the Prisma docs they have this section called operating against partial structures of your model types and then they have this table name get payload so any table defined inside of your schema it's going to exist on Prisma dot table name get payload and then you can pass

Chapter 12 6:58

Omit

Chapter 13 7:20

Other Utility Types

Chapter 14 7:36

Prisma Utility Types

in your specific query so in my example if I say Prisma restaurant get payload and then pass in the exact same include query that I'm including in this Prisma query here this is actually going to give me back a restaurant type that represents that specific type so like I said if you're inside of Prisma you technically wouldn't need these types of helpers but I've worked with libraries that don't have built-in helpers like this and I've had to reach for things like omit and pick and partial and different things like that now if you want to keep upping your typescript game definitely check out type challenges so this is a GitHub repo made by amp Fu and uh it's awesome you I would definitely say if you're newer to typescript start on the easier ones but this is going to teach you about a bunch of other built-in utility types and some of the more common things that you might do with typescript and also check out type hero so this is by the wonderful trash Dev and all of his community and you actually have type challenges that you can do to learn about generics and other built-in types and stuff like that as well if you have any other utility types that you use often and you think people should know about let us know down in the comments but that's all I got for you now I'll see you in the next one

Chapter 15 8:14

TypeScript Practice

Chapter 16 8:38

Thanks!

[Music]

Quote it with credit and a link. Quoting Syntax