Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Use native Object.assign when available. Fixes #54. #56

Closed

Conversation

mwistrand
Copy link

  • lang.assign delegates to Object.assign when avaialble.
  • Implement has('object-assign') test.

* `lang.assign` delegates to `Object.assign` when avaialble.
* Implement `has('object-assign')` test.
target: target
});
}
export let assign = has('object-assign') ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative way of doing this, instead of the any cast (that allows the interface to be preserved too) is the following:

interface ObjectAssignConstructor extends ObjectConstructor {
    assign(target: {}; ...sources: {}[]): {};
}

function assign(target: {}; ...sources: {}[]): {} {
    return (typeof (<ObjectAssignConstructor> Object).assign === 'function') ? (<ObjectAssignConstructor> Object).assign.apply(null, arguments) : _mixin({
        deep: false,
        inherited: false,
        sources: sources,
        target: target
    });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also, export const right?

@kitsonk
Copy link
Member

kitsonk commented Jul 18, 2015

@mwistrand the problem you are running into is what I ran into on microsoft/TypeScript#3889. It isn't static methods, it is that you are trying to extend it within the module and you cannot access the global namespace for interfaces from within a module.

* Remove dead code.
* Implement local `ObjectAssignConstructor` interface to preserve method signature when `Object.assign` is used.
@mwistrand
Copy link
Author

@kitsonk Thanks for passing along that TS issue. I was partly hoping that built-in interfaces could be opened up for extension (like modules/classes in Ruby), but that's clearly not the case. I'm not convinced, though, that declare var Object: ObjectAssignConstructor is more elegant than casting Object, so I stuck with your initial suggestion.

@kitsonk
Copy link
Member

kitsonk commented Jul 19, 2015

I think they will be, once microsoft/TypeScript#983 is solved. You can extend them if you aren't in a module. The other solution is to add them to the projects .d.ts, but then you are "changing" the global Object for that project, which you might not actually want to do. So the "limitation" in this case actually benefits I think for feature detection.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants