Is the Property Decorator failing to substitute the definition?

My code is similar to the following scenario, where I am attempting to replace a property based on a decorator

export function DecorateMe() {
    return function(component: any, propertyKey: string) {
        Object.defineProperty(component, propertyKey, {
            get: () => {
                ...
            },
            set: (value: boolean) => {
                ...
            }
        });
    };
}
class Parent {}

@Component()
class Child extends Parent {
    @DecorateMe()
    public property: string;
}

However, during runtime, I am observing that property is being defined as normal, thus hiding the added get/set methods in the prototype.

Based on resources like the TypeScript documentation and this tutorial, I was under the impression that this should work.

Since I am using Angular, I am uncertain if that could be a contributing factor influencing the behavior.

Answer №1

Seems like your current function is not suited for property decorators. Try using the following code snippet:

export function ApplyDecorator(target: any, propertyName: string) {
   var _value = target[propertyName];

  if (delete target[propertyName]) {
    Object.defineProperty(target, propertyName, {
        get: () => {
            console.log("Getter invoked ", _value);
            return _value;
        },
        set: (newVal: any) => {
            _value = newVal;
            console.log("Setter invoked ", _value);
        }
    });
  }
}

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

The 'Group' type is lacking the 'children' properties needed for the 'Element' type in Kendo UI Charts Drawing when using e.createVisual()

In my Angular 10 project, I utilized the following function to draw Kendo Charts on Donut Chart public visual(e: SeriesVisualArgs): Group { // Obtain parameters for the segments this.center = e.center; this.radius = e.innerRadius; // Crea ...

Using React with Typescript: Can the parent component access data fetched from a nested child component?

Can I access data retrieved from a child component (using a graphql query) in a parent component? For example, how can I use the data fetched by React-component-4 in React-component-1? Is there a way to do this or do I have to duplicate the data fetching ...

Incorporate TypeScript with Angular 2 to construct a dictionary with <key, value> pairs. Then, utilize a JSON file to populate the dictionary with data

I am in need of assistance with creating a dictionary or list with a specific structure. I have prepared a json file within my angular 2 project, and I would like to load this data into the dictionary that I am creating. Sample content from my Json file: ...

Setting up next-i18next with NextJS and Typescript

While using the next-i18next library in a NextJS and Typescript project, I came across an issue mentioned at the end of this post. Can anyone provide guidance on how to resolve it? I have shared the code snippets from the files where I have implemented the ...

Angular2 context refers to the 'from' method of Observable

Within the constructor of my Angular2 component class, I have included the following code: Observable.from([1,2,3]).subscribe(e=>{ console.log(e); }); I have made sure to import the necessary modules: import { Observable } from ' ...

Ways to monitor a variable for changes and automatically update the value in related components

I feel like I may not be following best practices, so I'm hoping for some guidance. Within a provider, there is a variable that I have defined. @Injectable() export class SharedService { public mynumberservice:any=0; In both the MainComponent an ...

Flattening an array of Map in Typescript involves combining all the

I am working with an array containing entries of type Map<string, number> Is there a way to flatten this array into a single map? Map<string, number>[] converted to Map<string, number> Appreciate any help on this matter. ...

Tips for utilizing ng-template in various components

Is there a way to display an <ng-template> in different components? For example, let's take the component test with the following structure: test.html <ng-template #content > <p> Hello world </p> </ng-template> test. ...

Optimal approach for designing interfaces

I have a situation where I have an object retrieved from the database, which includes assignee and author ID properties that refer to user objects. As I transform a number into a user object, I am unsure about the best practice for defining the type of the ...

Exploring Several Images and Videos in Angular

I'm experiencing a challenge with displaying multiple images and videos in my Angular application. To differentiate between the two types of files, I use the "format" variable. Check out Stackblitz export class AppComponent { urls; format; on ...

Executing an action in a child component when a button is clicked on the parent route

Currently, I am developing a web application using Angular 4 that involves both parent and child routes. Within the parent route, there are two buttons available - 'Add' and 'Remove'. I am seeking guidance on how to trigger a function ...

Tips for incorporating jQuery UI into Angular 6

No matter how many methods I have tried from StackOverflow, I cannot seem to get jquery-ui to work in my angular 6 component. Here's what I've attempted: I went ahead and ran npm install jquery jquery-ui to install both jquery and jquery-ui. ...

Running the nestjs build command is impossible without the existence of the node_modules folder

Currently, I am in the process of creating a Nestjs micro-service and everything is going smoothly. To run the build found within the dist folder, I use the command below: node dist/main.js However, I encountered a problem where this command does not exec ...

Creating an Increment and Decrement Button for Products in Angular and Ionic 3

I have gone through various tutorials, but there is still some confusion. I am attempting to create an increment and decrement button, but so far I have not been successful. Within an ion-list, I have multiple h3 elements. Here is a snippet of my view: ...

Creating a FormGroup dynamically using an observable: A step-by-step guide

My current project involves creating a page with multiple reactive forms, tailored for different countries. These forms are generated based on JSON arrays received from the backend, allowing users to view and update settings individually. As I am uncertain ...

Using Vue.js 2 on multiple HTML pages with Typescript and ASP.Net Core

My ASP.Net Core MVC project utilizes VueJs2 for more complex tasks, with each view having its own corresponding js file. The directory structure is as follows: ├ Controllers\HomeController.cs (with actions Index & Details) ├ Scripts\Hom ...

A Guide to Implementing Inner CSS in Angular

I am working with an object named "Content" that has two properties: Content:{ html:string; css:string } My task is to render a div based on this object. I can easily render the html using the following code: <div [innnerHtml]="Content.html"& ...

Issue encountered during the creation process of a new component within Angular 4

While attempting to create a new component named signup-form using the command: ng generate component signup-form / ng g component signup-form An error is being thrown that reads: Unexpected token / in JSON at position 1154 The source of this error i ...

The term 'string' is typically employed as a data type, yet in this instance it is being utilized as an actual value

Just started working with TypeScript and encountered an issue while trying to set the state. Encountered this error message: 'string' is a type and cannot be used as a value here. const state = reactive({ user: { uid: "", ...

Guide on creating and deploying an iOS API file onto a physical device with a Mac computer

I recently switched to using a Mac and have installed Xcode successfully, along with adding the platform for iOS. However, when I use adb devices, my iPhone device is not detected, but my Android device is recognized when connected. Additionally, when ru ...