question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Way of specifying non-enumerable properties

See original GitHub issue

Object assign is defined like below in TS:

interface ObjectConstructor {
    /**
      * Copy the values of all of the enumerable own properties from one or more source objects to a
      * target object. Returns the target object.
      * @param target The target object to copy to.
      * @param source The source object from which to copy properties.
      */
    assign<T, U>(target: T, source: U): T & U;
}

Though from MDN it is only copying members that are enumerable:

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

So if U above has non-enumerable members, TS will copy them anyway. This is slightly incorrect and unsafe.

One issue I recently ran into was

const selection = Object.assign({}, window.getSelection());

I was assuming I was copying all members of the Selection object to {}:

interface Selection {
    readonly anchorNode: Node;
    readonly anchorOffset: number;
    // etc ...
}

declare var Selection: {
    prototype: Selection;
    new(): Selection;
}

Though it didn’t copy any members at all, because the Selection object only contains non-enumerable members.

Proposal

Mark properties as non-enumerable

interface Selection {
    readonly nonenum anchorNode: Node;
    readonly nonenum anchorOffset: number;
    // etc ...
}

And have an operator to get the only “enum side” of a type:

interface ObjectConstructor {
    /**
      * Copy the values of all of the enumerable own properties from one or more source objects to a
      * target object. Returns the target object.
      * @param target The target object to copy to.
      * @param source The source object from which to copy properties.
      */
    assign<T, U>(target: T, source: U): T & enumsof U;
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:46
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
RyanCavanaughcommented, Aug 18, 2016

Seems like kind of an edge case; I wouldn’t want to have to think about this for every .d.ts file for the sake of a couple of APIs

13reactions
jwgmeligmeylingcommented, Mar 11, 2020

I’d like to see the nonenum keywaord as well. It’s not only relevant for Object.assign, but also Object.keys(), Object.values(), JSON.stringify, etc. I’m not sure though if the enumsof is really relevant though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enumerability and ownership of properties - JavaScript | MDN
Every property in JavaScript objects can be classified by three factors: Enumerable or non-enumerable;; String or symbol;; Own property or ...
Read more >
Enumerability of properties • Deep JavaScript - Exploring JS
Object.keys() ignores non-enumerable properties. Object.getOwnPropertyNames() lists the string keys of all own properties. However, Reflect methods (such as ...
Read more >
Any way to traverse all the non–enumerable properties?
You can make it iterable using Symbol.iterator and define your own implementation. In the example below, I'm yielding the value of each key ......
Read more >
Non-enumerable Properties | Have Fun Learning
I was trying to figure out a way to enumerate through properties of Math ... By definition, when you define a non-enumerable property, ......
Read more >
How to make a property non-enumerable in JavaScript?
You can enumerate all properties and read their values using the for loop or Object.values() method as shown below, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found