microsoft / microsoft/TypeScript
Way of specifying non-enumerable properties
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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;
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ObjectConstructor declaration for Object.assign and the Selection example, then compare the proposed nonenum marker and enumsof operator with JavaScript's enumerable-own-property behavior. Done means the type system has an agreed way to represent enumerable properties and Object.assign no longer overstates copied members, with coverage for the shown case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100