Object destructuring in TypeScript

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
I spent a good 3 hours trying to figure out why my site wasn't deploying.
I thought of Typescript during that time, and even more when I found out what the issue was
Was it something that TypeScript would have caught? It can really help showing errors early on.
Yes it was. The moment didit3,Typescript would havewarmedme. I had some values missing from a function but it was working
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

The cool part about TypeScript is that we can define types for certain variables. However, there is a scenario that might prove a bit difficult.
And this is destructuring an object.
Let's take the following example:
const user = {firstname: 'Chris', lastname: 'Bongers', age: 32};
const {firstname, age} = user;
By using this destructuring, we extract specific properties from an object.
But how do we now define the types for this destructured object?
You might immediately think the following will work:
const {firstname: string, age: number} = user;
But this assigns the firstname variable to be string and the age variable to be called number.
And when we introduce two of the same type, we are hit with an error since we are redefining a variable.
This is because when we destructure an object, we can rename the properties like so:
const {firstname: userFirstname, age: userAge} = user;
To define these types, we have to assign them after the destructuring.
Which would look like this:
const {firstname, age}: {firstname: string, age: number} = user;
Do note you can still rename the variables, and we must still use the types for the original names.
const {firstname: userFirstname, age: userAge}: {firstname: string, age: number} = user;
We can make this a bit nicer by using TypeScript interfaces.
interface User {
firstname: string;
age: number;
}
const {firstname, age}: User = user;
That looks way nicer, right?
And there you go the correct way to typecast a destructured object in TypeScript.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter