Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if typescript class has setter/getter

I have a typescript class which has the following properties:

export class apiAccount  {
    private _balance : apiMoney;
    get balance():apiMoney {
        return this._balance;
    }
    set balance(value : apiMoney) {
        this._balance = value;
    }

    private _currency : string;
    get currency():string {
        return this._currency;
    }
    set currency(value : string) {
        this._currency = value;
    }
    ...

I need to create a blank instance of this class:

let newObj = new apiAccount();

And then check if it has the setter for "currency", for example. I thought that this is exactly what getOwnPropertyDescriptor does, however apparently I was wrong:

Object.getOwnPropertyDescriptor(newObj, 'currency')
Object.getOwnPropertyDescriptor(newObj, '_currency')

These both return undefined. But chrome seems to do it! When I hover over the instance, it shows me the properties, and shows them as undefined. How can I get a list of those property names, or check if the property descriptor exists in the object? enter image description here

like image 624
David Avatar asked Jun 05 '16 14:06

David


People also ask

Can we use getter setter in TS?

In TypeScript, there are two supported methods getter and setter to access and set the class members. In this very short article, I'm going to show you Typescript Accessor which includes getters/setters method. Actually, getters and setters are nothing but a way for you to provide access to the properties of an object.

How do you define a getter and setter in TypeScript?

Use the get and set keywords to define getters and setters in TypeScript. Getters enable us to bind a property to a function that is called when the property is accessed, whereas setters bind a property to a function that is called on attempts to set the property.

Is accessor getter or setter?

For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively. The getter method returns the value of the attribute.

What is property in TypeScript?

Property in TypeScriptA property of a function type for each exported function declaration. A property of a constructor type for each exported class declaration. A property of an object type for each exported internal module declaration.


1 Answers

The "problem" is that Object.getOwnPropertyDescriptor - as the name implies - only returns descriptors of an object's own properties. That is: only properties that are directly assigned to that object, not those that are from one of the objects in its prototype chain.

In your example, the currency property is defined on apiAccount.prototype, not on newObj. The following code snippet demonstrates this:

class apiAccount {
    private _currency : string;
    get currency():string {
        return this._currency;
    }
    set currency(value : string) {
        this._currency = value;
    }
}

let newObj = new apiAccount();
console.log(Object.getOwnPropertyDescriptor(newObj, 'currency')); // undefined
console.log(Object.getOwnPropertyDescriptor(apiAccount.prototype, 'currency')); // { get, set, ... }

If you want to find a property descriptor anywhere in an object's prototype chain, you'll need to loop with Object.getPrototypeOf:

function getPropertyDescriptor(obj: any, prop: string) : PropertyDescriptor {
    let desc;
    do {
        desc = Object.getOwnPropertyDescriptor(obj, prop);
    } while (!desc && (obj = Object.getPrototypeOf(obj)));
    return desc;
}

console.log(getPropertyDescriptor(newObj, 'currency')); // { get, set, ... }
like image 116
Mattias Buelens Avatar answered Nov 07 '22 02:11

Mattias Buelens