There's nothing strange in that, you'll have to understand Prototype vs Instance properties
makeEnumerable
sets enumerable descriptors on the instance.enumerable
decorator modifies prototype-level descriptors.You are expecting Object.keys(enumerableA)
to be ['a', 'b']
, like 'forin': keys
, but:
for...in
loop iterates over both own and inherited enumerable properties.Object.keys
returns only it's own enumerable properties.Check this MDN blog for more info. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties#querying_object_properties
for (const key in enumerableA)
['a', 'b']
for...in
loop iterates over both own and inherited enumerable properties.b
and a
are enumerable but on different level. b
is made enumerable by makeEnumerable
as instance property and a
prototype property made enumerable by the @enumerable
decorator.Object.keys(enumerableA)
['b']
Object.keys
lists only the own enumerable properties.b
is made an own enumerable property by makeEnumerable
function in constructor.a
is still on the prototype, so it is excluded.Object.keys(Object.getPrototypeOf(enumerableA))
['a']
@enumerable
decorator modifies the prototype-level descriptor for a
.b
is non-enumerable on prototype because makeEnumerable
function made enumerable on instance only.Object.getOwnPropertyNames(enumerableA)
['b']
b
is an own property on the instance.Object.getOwnPropertyNames(Object.getPrototypeOf(enumerableA))
['constructor', 'a', 'b']
constructor
, and b
are non-enumerable but exist on prototype.Object.entries
throws an errorObject.entries
access all the enumerable own properties.
Object.entries(enumerableA)
:
b
as it is enumerable property on instance. While accessing b
, the this
context of get b() {...}
is the instance of MyClass { b: [Getter/Setter] }
.Object.entries(Object.getPrototypeOf(enumerableA))
a
because a
is an enumerable property on the prototype.this
context for the get a(){...}
is the prototype object ({ a: [Getter/Setter] }
), not an instance of MyClass
You must understand how private properties are handled by typescript.
this
when a method is called. If this
is not same as own class
it throws an error.No, it is not possible to make instance properties enumerable directly using decorators in Typescript because property decorators in Typescript only have access to the class prototype
for instance members, not the instance itself.
instance properities
enumerable use makeEnumerable
function, as you used for b
.I hope, I've addressed all your issues. If anything else you'd like to clarify, feel free to ask. Happy learning!