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

Syntax Video

5 NEW Features in TypeScript 5.5

Chapter 1 0:00

Intro

typescript 5.5 RC was just announced a few days ago and in this video I'm going to show you the five features that I am excited about now definitely check out the blog post that goes into everything but the first thing I'm excited about is inferred type predicates so let's see a code example so let's say you have a map here that's pulling in some data that could actually be either a string or undefined and so the result of this map is string or undefined we then want to get rid of those undefined so we filter them out we basically say if it's undefined get rid of it and now the return of this filter will be a list that only has strings inside of it but you can see that the inferred type right now is string or undefined array and so later on if I try to use it we're going to get a type error that says user is possibly undefined now with the latest version of typescript it's able to see this and infer it as a type predicate and the return value of this function is now an array of strings instead of an array of string or undefine and so our code down below no longer gets a type error now the way that this is working like I said is with type predicate so if

Chapter 2 0:09

Inferred Type Predicates

you're new to that I'm going to link the handbook so you can read more about it and if you look at the blog post they have the specific rules that you need to follow in order for your code to be detected as a type predicate in order for this feature to work now next up is control flow narrowing for constant index accesses uh let me show you what this entails so let's say you're working on an app and maybe you are trying to format specific um properties on some kinds of objects and so here I have a product that can have a title or price but you can see both of them could possibly be undefined and then down here I'm calling my format function I'm passing in that product and then I'm passing in the specific prop of the thing that I want to format now if we look at the function implementation first we make sure that that value actually exists inside the product right because both title and price could be undefined so we make that check then we check to see if it's a string we want to call two uppercase so this is like our our formatter function in this case then if it's a number we want to call two fixed but you can see right here typescript still thinks that this could be a string or a number and that's because we're using bracket

Chapter 3 1:10

Improved Indexed Access

notation right here so it it wasn't smart enough to be able to look past that bracket notation to understand what the inferred type should be but in the latest version of typescript you will see that now we don't get that error anymore and basically it's able to infer the type using bracket notation so it Narrows the type to see that it's not undefined it Narrows the type to see that it's a string and then right here we can call two uppercase because it is a string and here we can call two fix because it is a number definitely check out the pr that talks about uh different use cases and also shows how they implemented this because it's pretty cool and I'm excited about that now another thing coming is type Imports in JS stock so before if you ever use JS stock you typically would have to have like a direct import inside of your js.

Chapter 4 2:30

Type Imports in JSDoc

comments because you don't want to have a full import because that would included get get included in your bundle and so what you often have is a code base where if a specific module is importing from multiple things in it's JS St you have Imports in multiple places now you could wrap that up to typed def but that also gets repetitive with the latest version of typescript they've introduced the at import comment tag so now you can actually import a full library in your JS do and then just use that thing inside of your JS do comments and so you can do named Imports you can do namespace Imports it's all supported here and that's definitely going to clean up some js do so I'm excited about that now the next thing I'm excited about is support for new ecmascript set methods now if you didn't know set methods for JavaScript is already a stage four proposal for ecmascript so it's already been merged in it will be in the latest version of ecmascript released this year but you get a bunch of methods for doing set operations and uh if you're not familiar with these definitely check out the mdn article it has some visual representations of what these set methods do but this is pretty exciting because now we have the type definitions

Chapter 5 3:12

New Set Methods

for them we can use them in typescript and if you like doing code challenges these are going to be very useful for uh things like leak code or code Wars where you very often have to do like set operations and the last thing I'm super excited about that's definitely going to clean up some code bases is the config D template variable in configuration files so with typescript typically you might have in a in a mono repo you might have base config that you define somewhere and then all of your other projects in the monor repo extend from that base config but one of the issues with that is if there are any folder paths in that base config these are all relative to the base config so they have to be overridden in your sub projects now the base config can use this config dur template variable and this will be relative to where the uh base config is being extended from So You Define it like this in your base config and now anywhere that extends it these file paths will be relative to the file that is extending it so this is going to definitely clean up some TS configs because very often you had to basically have duplicate declarations in your base

Chapter 6 3:49

configDir Template Variable

config and in your project configs all right that's it for the stuff that I'm excited about definitely check out the blog post to see the rest of the things that are coming to typescript 5.5 and if you're curious when you can use it you can check out their iteration plan right now the planned release is June 18th that could change uh but it's looking like we're about a week away from being able to use this in your production code bases which is pretty exciting all right that's all I have in this video I'll see you in the next one

Chapter 7 4:39

When can I use this?

Quote it with credit and a link. Quoting Syntax