[英]Using TypeScript, and Object.assign gives me an error “property 'assign' does not exist on type 'ObjectConstructor'”
I am writing my question again because earlier it made little sense and I wasn't very clear. 我再次写我的问题,因为早些时候它没有意义,我不是很清楚。
I am receiving data from API that looks something like this: 我从API接收的数据看起来像这样:
{"photos":[{"id":1,"title":"photo_1_title"}]}
So, in my code, I have a photos
variable, and a method called getPhotos()
所以,在我的代码中,我有一个
photos
变量,以及一个名为getPhotos()
的方法
I am using infinite scroll so when I reach the bottom of the page, I call getPhotos()
again. 我使用无限滚动,所以当我到达页面底部时,我再次调用
getPhotos()
。
photos: any;
getPhotos() {
this.photoService.getPhotos()
.subscribe(
photos => this.photos = photos
// here, instead of doing this, I want to add the array of photos I get back to this.photos using Object.assign however it is giving me the said error
)
}
So if the next time I call it, I get back {"photos":[{"id":2,"title":"photo_2_title"}]}
, then I am trying to set this.photos
to be 因此,如果下次我打电话给它,我会回来
{"photos":[{"id":2,"title":"photo_2_title"}]}
,然后我试图设置this.photos
为
{"photos":[{"id":1,"title":"photo_1_title"}, {"id":2,"title":"photo_2_title"}]}
can someone help me with why 谁可以帮助我为什么
jsfiddle.net/ca46hLw9 doesn't work? jsfiddle.net/ca46hLw9不起作用? I thought assign is supposed to merge contents of an object together right?
我认为assign应该合并一个对象的内容吗?
Object.assign
is an ECMAScript2015 feature and does not exist in ECMAScript5 and lower. Object.assign
是ECMAScript2015功能,在ECMAScript5及更低版本中不存在。
You're most likely targeting to compile your Typescript for ECMAScript5 and therefor the Object interface does not have assign
defined. 您最有可能的目标是为ECMAScript5编译您的Typescript,因此Object接口没有定义
assign
。
You can either target ECMAScript2015 by changing your TS compiler configuration with 您可以通过更改TS编译器配置来定位ECMAScript2015
target: 'es6'
or you can extend the ObjectConstructor interface with the method assign
或者您可以使用方法
assign
扩展ObjectConstructor接口
declare interface ObjectConstructor {
assign(...objects: Object[]): Object;
}
(you can add this declaration anywhere, redeclaring an interface extends it instead of overwriting it) (你可以在任何地方添加这个声明,重新声明一个接口扩展它而不是覆盖它)
Or you can coerce Object
to any
and ignore its typing: 或者您可以将
Object
强制转换为any
并忽略其输入:
(<any>Object).assign( this.photos, photos )
Whichever you choose, keep in mind that if you want this to run on older browsers you need to add a polyfill. 无论您选择哪种方式,请记住,如果您希望在旧版浏览器上运行此功能,则需要添加polyfill。 Most modern browsers are fairly close to implementing the ES2015 feature set, but they're not 100% there, meaning that some parts of your code will still fail if you rely on ES2015 functionality.
大多数现代浏览器都非常接近于实现ES2015功能集,但它们并非100%存在,这意味着如果您依赖ES2015功能,代码的某些部分仍然会失败。
Typescript will not polyfill Object.assign
when targetting ES5 or older, that is why you are getting this error. 在目标ES5或更早版本时,Typescript不会对
Object.assign
填充,这就是您收到此错误的原因。 On the other hand, Babel does have polyfills in the form of plugins, which would mean you need to incorporate Babel transpilation into your build pipeline, see: https://babeljs.io/docs/plugins/transform-object-assign/ 另一方面,Babel确实有插件形式的polyfill,这意味着您需要将Babel transpilation合并到您的构建管道中,请参阅: https ://babeljs.io/docs/plugins/transform-object-assign/
As a final option you can consider using a library as Lodash or jQuery, which have their own implementation of Object.assign
(called extend
) 作为最后一个选项,你可以考虑使用一个库作为Lodash或jQuery,它们有自己的
Object.assign
实现(称为extend
)
With Typescript 2.1 or higher, you can use Object spread syntax instead. 使用Typescript 2.1或更高版本,您可以使用Object spread语法。
let obj = { x: 1, y: "string" };
var newObj = {...obj, z: 3, y: 4}; // { x: number, y: number, z: number }
The order of specifying spread operations determines what properties end up in the resulting object;
指定扩展操作的顺序决定了结果对象中最终的属性; properties in later spreads “win out” over previously created properties.
后来的点差中的属性“赢出”以前创建的属性。
For typescript version 2.0 or higher, just modify the tsconfig.json file so the "lib" section includes "es6": 对于打字稿版本2.0或更高版本,只需修改tsconfig.json文件,使“lib”部分包含“es6”:
"lib": [
"es5",
"es6",
"dom",
"es2015.collection"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.