How to destructure properties from an object in Typescript, including its internal properties

Here is a snippet of the code I am working with:

const { person: { name, age }, house: { height } } = myObject;

// Works
console.log(name);

// Does not work
console.log(person);

The issue arises when I need to have both an individual variable name, nested within person, and a separate variable person.

Answer №1

The solution requires the specific mention of 'person' twice in the code:

const { person, person: { name, age }, house: { height } } = myObject;

// Success
console.log(name);

// Failure
console.log(person);

This approach appears to be effective

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Disable the loader for a specific method that was implemented in the Interceptor

Custom Loader Interceptor A loader has been implemented within an Interceptor. I have a specific requirement where the loader should not be triggered during the upload() function. It should not be applied to that particular method only. ...

In a Next.js project, Typescript seems to be overlooking errors related to proptype definitions and function types

Greetings everyone! I am currently working on a project using TypeScript and have implemented various rules and elements. However, I am facing issues with type errors for functions and props. Essentially, when using any function, it is necessary to specify ...

Error in Astro Build: Markdown integration with Typescript has encountered a problem

Encountering an error while trying to build my Astro Project using npm run build, or when navigating to one of my markdown pages after running: npm run dev. The error message is as follows: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Verify that the user's input falls within a specified range before adding it to the result

Currently, I'm trying to implement validation on user input. The idea is that if a user enters a number between 1 and 10, I want to add a 0 in front of it. For example, if the user inputs 4, I would like to store the value 04. While I am comfortable d ...

Angular - Using ng-pick-datetime to set initial value in a reactive form

Currently, I am in the process of developing a form and utilizing a popular date time picker plugin. Despite my attempts to initialize the value of the DateTime input, I have not been successful and even the default value is not being set in the form. If ...

Is there a way to determine the specific type of a property or field during runtime in TypeScript?

Is there a way to retrieve the class or class name of a property in TypeScript, specifically from a property decorator when the property does not have a set value? Let's consider an example: class Example { abc: ABC } How can I access the class or ...

Expanding the number of buttons for <ul> in creating a responsive navigation system using Angular

My navigation consists of 8 items (li), and as the resolution shrinks, the items are pushed onto a new line. I am looking to implement a feature where if an item doesn't fit in the navigation anymore, a "MORE" dropdown button appears on the right side ...

Is it possible for a conditional type to dictate the type it is connected

I'm currently attempting to utilize conditional types with generics in order to determine another type, but I am facing issues with the narrowing of the conditional type. Specifically, I am looking to narrow down the conditional type based on the para ...

Why do rows in the React-bootstrap grid layout overlap when the screen is resized?

I am working on a simple layout structure with 2 rows: Row 1 consists of 2 columns Row 2 consists of 1 column The goal is to have both rows expand in width and avoid vertical scrolling of the page. However, when resizing the screen, I want the columns of ...

The Angular 2 router UMD file, router.umd.js, was not found

Trying to run an Angular 2 project and implement @angular/router is proving to be a bit challenging. Everything seems to be working fine, until the moment I attempt: import { provideRouter, RouterConfig } from '@angular/router'; As it tries to ...

Incorporating an offset with the I18nPluralPipe

Having trouble with my multiselect dropdown and the text pluralization. I attempted to use the I18nPluralPipe, but can't seem to set an offset of 1. ListItem = [Lion, Tiger, Cat, Fox] Select 1 Item(Tiger) = "Tiger", Select 3 Item(Tiger, Cat, Fox) = ...

What is preventing me from invoking an overloaded function using this approach?

Consider this scenario involving function overloading: function selectItem(x: {type: string; id: number; }[]): number; function selectItem(x: number): {type: string; id: number; }; function selectItem(x): any { /* ... */ } The documentation explain ...

What is the importance of typescript requiring the use of generics?

const typeFunction = <T>(a:string, {c,d} = {c: T[], D:T} = {})=>{} In what way does TypeScript enforce the usage of generics? I am looking to create a function that requires a specific type (T) to be passed in when it is used. typeFunction<st ...

get those with a `date` that falls within the past 6 minutes

In my MongoDB database, I have a collection of Product items. Each product has an attribute called date: const productSchema = new mongoose.Schema<ProductAttrs>({ date: { type: Date, required: true, } ... }) I'd lik ...

Throttle user input before making an HTTP request

Currently, I have set up a 2-way binding for the input to appRequestParams.appName. The fetchApps() method is called on every keyup event. My goal is to debounce the input so that backend HTTP requests are not triggered immediately upon every keyup. I ha ...

What method does Angular use to distinguish between familiar elements and unfamiliar ones?

<bar>- it is a tag with a random name, not officially recognized, so it makes sense that it is considered invalid <div> is valid because it is a standard HTML element - does Angular have a documented list of standard elements? Where can I fin ...

How come the props aren't being passed from the parent to the child component? (React / TypeScript)

Learning TypeScript for the first time and facing an issue with passing props from parent to child components. The error seems to be related to the type of props, but I'm not sure how to fix it since all types seem correct. Error message: "Property ...

Show the name of the category in the mat table in Angular instead of the category

Using Angular Material Mat Table to display data fetched from an API is great, but I'm facing a challenge with handling categories from another API. Currently, my models include issues and categories, where the category ID is displayed instead of the ...

Improving the App() function in React and Typescipt

When working on my React/Typescript app, I encountered a challenge with the length of the code in one of the sections. Despite watching tutorials and searching for solutions, I couldn't find a clear way to refactor it. The specific issue is with refa ...

Unidentified class in Typescript

Does anyone know how to create an anonymous class in TypeScript? Here is my code: export abstract class Runnable { public abstract run(); } I'm attempting to achieve something like this: new Runnable { runner() { //implementation ...