The TypeScript 5.3 Feature They Didn't Tell You About
TypeScript 5.3 introduces relaxed rules around readonly arrays and improvements in const type parameters.
As part of my work as a TypeScript educator, I get asked a lot about the TypeScript utility types - especially about how to use them in application code. So, I'm starting a series on them - built around real-world examples.
Let's start with the Exclude type helper.
tstypeFruit = "apple" | "banana" | "orange";typeResult =Exclude <Fruit , "orange">;
We can use Exclude to remove a single member of a union. The first argument represents the full union, and the second argument represents the member to remove.
Technically, the second argument can be any type - TypeScript won't warn you if you try to remove a member that doesn't exist. It'll just return the original union.
tstypeResult =Exclude <Fruit , "pear">;
tstypeEvent = "click" | "focus" | "change" | "abort";typeClickAndFocusEvent =Exclude <Event ,"change" | "abort">;
We can also use Exclude to remove multiple members from a union. By passing a union to the second argument, we can remove multiple members at once.
Just like above, not all of those members need to exist in the original union:
tstypeClickAndFocusEvent =Exclude <Event ,"change" | "abort" | "blur">;
tstypeEvent =| {type : "click";}| {type : "focus";}| {type : "change";};
A discriminated union is a union, usually of objects, which have a common property that can be used to discriminate between them. In the example above, the type property is used to discriminate between the different events.
We can extract a subset of the union by using Exclude to remove all members that don't have a specific value for the type property.
tstypeEvent =| {type : "click";}| {type : "focus";}| {type : "change";};typeClickAndFocusEvent =Exclude <Event ,{type : "change" }>;
This works even if the members of the union have other properties attached to them.
tstypeEvent =| {type : "click";x : number;y : number;}| {type : "focus";}| {type : "change";value : string;};typeClickAndFocusEvent =Exclude <Event , {type : "click" }>; // { type: 'focus' } | { type: 'change', value: string }
In the example above, the x and y properties don't need to be passed to Exclude in order to remove the click event.
tstypeEvent =| {type : "click";}| {type : "focus";}| {type : "change";}| {type : "abort";};typeClickAndFocusEvent =Exclude <Event ,{type : "change" } | {type : "abort" }>;
You can also remove multiple members of a discriminated union by passing a union to the second argument. This can either be a union of the members of the union, or a union of the type property:
tstypeClickAndFocusEvent =Exclude <Event ,{type : "change" | "abort" }>;
tstypeRoutes =| {route : "/user";search : {id : string;};}| {route : "/user/create";}| {route : "/user/edit";search : {id : string;};};typeRoutesWithoutSearch =Exclude <Routes ,{search : any;}>;
You don't need to include the 'discriminator' (in this case, route) in the second argument to Exclude. You can just pass the shape of the members you want to remove.
In the example above, we're removing all members of the Routes union that have a search property.
tstypePossibleTypes = "admin" | "user" | 0 | 1 | 2;typeStringTypes =Exclude <PossibleTypes , number>;
Exclude also works on basic types. In the example above, we're removing all literals that match number from the PossibleTypes union.
This can be useful if you want to remove all strings from a union, or all numbers, or all booleans.
tstypeObjectKey =| "userId"| "postId"| "userName"| "postName";typePostKey =Exclude <ObjectKey ,`${string}${"user"}${string}`>;
You can use Exclude to remove all strings from a union that contain a specific substring.
In the example above, we're removing all strings that contain the substring user from the ObjectKey union.
We use a template literal to represent the string we want to remove - in this case, user. We then use the ${string} syntax to represent any string that comes before or after the substring we want to remove.
tstypeObjectKey =| "userId"| "postId"| "id"| "userName"| "postName";typeNonIdKey =Exclude <ObjectKey ,`${string}${"id" | "Id"}${string}`>;
You can also use Exclude to remove all strings from a union that contain one of several possible substrings.
In the example above, we're removing all strings that contain either id or Id from the ObjectKey union. By passing a union to the template literal, we can remove multiple substrings at once.
tstypeObjectKey =| "userId"| "postId"| "id"| "userName"| "postName";typeNonNameKey =Exclude <ObjectKey , `${string}Name`>;
You can use Exclude to remove all strings from a union that have a certain prefix or suffix.
In the example above, we're removing all strings that end with Name from the ObjectKey union.
Here, ${string} is used to represent a string of any length that comes before the substring we want to remove.
To switch this over to match a certain prefix, we can move ${string} to the end of the template literal instead.
Exclude is a very powerful utility type that can be used in a variety of ways. If you have other examples of how you've used Exclude, let me know on my TypeScript Wizards Discord!
Share this article with your friends
TypeScript 5.3 introduces relaxed rules around readonly arrays and improvements in const type parameters.
Learn how to provide a TypeScript playground when asking for help with your TypeScript questions, making it easier for others to assist you.
Learn how to work with events in React and TypeScript, from onClick to onSubmit.
A step-by-step guide on setting up ESBuild to bundle a Node application.
When using '--moduleResolution' with the option 'nodenext', it is necessary to add explicit file extensions to relative import paths in EcmaScript imports.
Learn how to add TypeScript to your existing React project in a few simple steps.