-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any roadmap on supporting Object.assign? #3429
Comments
not sure what you are looking for. if you mean the typings, it is already defined in lib.es6.d.ts as /**
* 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 sources One or more source objects to copy properties from.
*/
assign(target: any, ...sources: any[]): any; i you mean the implementation, TypeScript does not include pollyfills, you can always include yours, for instance Mozilla has a pollyfill for it. If you are asking about the the mixin pattern typingings, it is something that we always had on the roadmap for 2.0. |
Thanks. I mean writing 👍 |
@unional I may have misunderstood something. But I don't think your conclusion is correct. ES6 anyway supports But if you transpile to es5 and below, you'd still need a polyfill, because es5 doesn't have As a quick workaround consider this polyfill and typing:
|
Thanks for digging up this issue. It was quite a while ago. :) Yes, I now understand that it simply requires a polyfill for es5. Thanks again! |
Will we compile this polyfill when target is set to es5? I mean, since the compilation target is set to es5, then this should be able to work under es5 environment. |
Yes, as a ponyfill 🌹 🐴 |
I'm sorry I don't understand. ES6 supports |
@prashaantt TypeScript doesn't provide any polyfills and it's by design. If you want to use |
@devoto13 Thanks, I just had a silly question. After |
@prashaantt Once |
Thanks @jesseschalken. As a follow up, isn't importing the whole |
@prashaantt It depends on the browsers you're targeting. You already know If you only want the polyfill for |
If you're already using Babel, I recommend using |
Thanks for the tips, though we should have full generators support any moment now — literally the last hurdle to 2.0! |
@prashaantt What do you mean when you say that Babel transpiles |
@aluanhaddad My understanding of what babel does is if you specify es5 as your target and use |
@devoto13 if your target is es5, then you should at the very least throw a warning that Object.assign isn't supported in es5. It makes zero sense to have it be completely valid and not telling the programmer that you need some random polyfill. |
@kyleholzinger what you've described (targeting ES5 means Object.assign isn't available) is already the behavior. |
@kyleholzinger It indeed does throw error. If you create folder with two following files:
And then run
In your project you probably include typings for some polyfill, but don't include polyfill implementation, that's why it fails. |
I understand that it's the current behavior haha. My point is that if you specify the target as es5, it'd be nice if typescript gave you a meaningful error beyond "it's not on the constructor". |
@kyleholzinger FWIW, TypeScript 2.1 now supports the ES6 (ES7?) object rest/spread so I personally find lesser reason to bother about |
That's true. It'd just be nice to not leave out language features. It'd be awesome if Object.assign wasn't browser dependent and typescript warned you if you weren't using a polyfill. |
If your tsconfig is set right, TypeScript warns you about |
right, my only point is in the es2015 spec, so it should be in typescript ;) |
How do I use |
@johnbendi Here is how you can do test if your runtime supports $ Node
> Object.assign({ to: 'world' }, { message: 'Hello there!' })
{ to: 'world', message: 'Hello there!' } If the above works all you need to do is include If it the above fails, add a polyfill like this one, which is written in TypeScript. // polyfill-object-assign.ts
if (typeof Object.assign !== 'function') {
Object.assign = function (target, ...args) {
if (!target) {
throw TypeError('Cannot convert undefined or null to object');
}
for (const source of args) {
if (source) {
Object.keys(source).forEach(key => target[key] = source[key]);
}
}
return target;
};
} And import it with import './polyfill-object-assign'; And also make the same changes to your tsconfig.json as for the runtime supported case. I hope that helps |
@aluanhaddad thanks a lot for the insights. My node does support |
@aluanhaddad never mind. It works well now. |
@aluanhaddad I previously had
Sample of my new errors:
So is there a way to use your recommendation and still get typescript to compile correctly? |
@johnbendi yes, certainly. Use I was simply suggesting adding the specific entry |
I think what's missing is a clean way to say "I have an es6 polyfill and it's not your bussiness typescript, just assume I do" :). Because, setting Requiring core-js explicitly essentially forces you to have the shim, you can't have shimmy and non-shimmy builds because TS will complain. Adding |
@himdel just use {
"compilerOptions": {
"lib": [
"es2015"
]
}
} It works perfectly - I've been doing it for months. |
Or any subset you wish: {
"compilerOptions": {
"lib": [
"es2015.core",
"es2016.array.include"
]
}
} |
I still cannot figure out how to do this. When i have In my case I want arrow functions to be converted to normal functions, but leave static calls like tsc should have a flag to tell it to only transpile syntax features and not polyfill features like static methods |
@danez TypeScript won't modify a call to |
@RyanCavanaugh It does. Babel is not involved in the process. var a = Object.assign({}, {}); into this var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var a = _assign({}, {}); |
@danez Can you post an actual repro? The compiler did not emit that code |
I don't have a repo, but this is the tsconfig: {
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"module": "ES2015",
"target": "es5",
"outDir": "js/lib/es"
},
"include": [
"js/**/*.ts",
],
"exclude": [
"**/__tests__/**/*.ts"
]
} and then i simply call |
@danez I'm afraid you'll have to post an actual repro.
|
Maybe related: #12901 |
@unional in that it is working properly. The compiler has a helper for providing syntactical support, but it does not ever rewrite functionality on globals. @danez that helper is not ever accessible by TypeScript code. const foo = { foo: 'bar' };
const bar = { ...foo }; will emit: var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var foo = { foo: 'bar' };
var bar = __assign({}, foo); I don't think you can provide an example where |
@kitsonk Yes I was using object spread, you are right. So would be nice if object-spread would just be simply transformed to |
@danez it is when you target something that supports For example, this: const foo = { foo: 'bar' };
const bar = { ...foo }; Will output: const foo = { foo: 'bar' };
const bar = Object.assign({}, foo); |
@kitsonk Yes I know, but I'm targeting es5 syntax with all ES2017+ globals being polyfilled by core-js. So what I'm referring to what would be nice is a mode that outputs es5 syntax but assumes all builtins are available. Similar to what babel is doing with the |
In general we don't go out of our way to give special support to "mixed" runtime targets, but there are other options available. You can inject |
I feel like this is the main problem with not shimming features like |
I can understand you feel it is confusing, but it isn't if you keep the mantra, TypeScript provides syntatical rewrites not functional polyfills. TypeScript is full consistent in how it behaves, and it is opinionated about what it does, and 99% of the time it has no impact on the end user. As @RyanCavanaugh says, it is possible to leverage a set of polyfills, plus __assign = Object.assign; But that is really "bonus round" TypeScript and arguable that it would provide any real world measurable performance improvement. |
in 1.6 or 2.0?
The text was updated successfully, but these errors were encountered: