Way of specifying non-enumerable properties
See original GitHub issueObject 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:
- Created 7 years ago
- Reactions:46
- Comments:11 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
I’d like to see the
nonenum
keywaord as well. It’s not only relevant forObject.assign
, but alsoObject.keys()
,Object.values()
,JSON.stringify
, etc. I’m not sure though if theenumsof
is really relevant though.