Using TypeScript, and Object.assign gives me an error "property 'assign' does not exist on type 'ObjectConstructor'"

user1354934

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:

{"photos":[{"id":1,"title":"photo_1_title"}]}

So, in my code, I have a photos variable, and a method called getPhotos()

I am using infinite scroll so when I reach the bottom of the page, I call getPhotos() again.

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":1,"title":"photo_1_title"}, {"id":2,"title":"photo_2_title"}]}

can someone help me with why
jsfiddle.net/ca46hLw9 doesn't work? I thought assign is supposed to merge contents of an object together right?

vileRaisin

Object.assign is an ECMAScript2015 feature and does not exist in ECMAScript5 and lower.

You're most likely targeting to compile your Typescript for ECMAScript5 and therefor the Object interface does not have assign defined.

You can either target ECMAScript2015 by changing your TS compiler configuration with

target: 'es6'

or you can extend the ObjectConstructor interface with the method assign

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:

(<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. 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.

Typescript will not polyfill Object.assign when targetting ES5 or older, that is why you are getting this error. 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/

As a final option you can consider using a library as Lodash or jQuery, which have their own implementation of Object.assign (called extend)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Property 'assign' does not exist on type 'ObjectConstructor'

From Java

Using Object.assign to avoid redundancy in Typescript constructors

From Dev

Selecting custom fields of "who" object using SOQL gives no such column error

From Dev

Using CURL and PHPSimpleHTMLDOMParser gives me - 500 Internal Server error

From Dev

using Get for ajax calls gives me 500 error

From Dev

Why doesn't TypeScript raise an error when I assign a number to an Object?

From Dev

Animate UITableViewCell gives me error

From Dev

Sorting array gives me error

From Dev

php header gives me the error

From Dev

Why Boolean null object gives me null pointer exception but String do not give me NPE while comparing using == operator

From Dev

Signature for Object.assign in typescript?

From Dev

php artisan route::list gives me an error: Trying to get property of non-object

From Dev

looks like a valid python syntax to me but gives me invalid error using in django

From Dev

Using a method a second time gives me an error (IOS)

From Dev

Typescript error TS1005: ':' expected. with Object.assign()

From Dev

.getDownloadUrl() gives me error - Android

From Dev

using "this" in a fragment gives me an error

From Dev

Selecting custom fields of "who" object using SOQL gives no such column error

From Dev

MySQL 'AND' gives me error

From Dev

using Get for ajax calls gives me 500 error

From Dev

It gives me an error at GPS

From Dev

Animate UITableViewCell gives me error

From Dev

Sorting array gives me error

From Dev

For Loop gives Object Required Error

From Dev

Firefox gives me dbus error

From Dev

Using str.replace gives me a TypeError: 'str' object cannot be interpreted as an integer

From Dev

Using 'import * as' in a typescript component gives an error -

From Dev

I implemented a Map object in C, but using it gives me a Segmentation Fault

From Dev

Yii2- Setting user id to null gives me non-object error

Related Related

  1. 1

    Property 'assign' does not exist on type 'ObjectConstructor'

  2. 2

    Using Object.assign to avoid redundancy in Typescript constructors

  3. 3

    Selecting custom fields of "who" object using SOQL gives no such column error

  4. 4

    Using CURL and PHPSimpleHTMLDOMParser gives me - 500 Internal Server error

  5. 5

    using Get for ajax calls gives me 500 error

  6. 6

    Why doesn't TypeScript raise an error when I assign a number to an Object?

  7. 7

    Animate UITableViewCell gives me error

  8. 8

    Sorting array gives me error

  9. 9

    php header gives me the error

  10. 10

    Why Boolean null object gives me null pointer exception but String do not give me NPE while comparing using == operator

  11. 11

    Signature for Object.assign in typescript?

  12. 12

    php artisan route::list gives me an error: Trying to get property of non-object

  13. 13

    looks like a valid python syntax to me but gives me invalid error using in django

  14. 14

    Using a method a second time gives me an error (IOS)

  15. 15

    Typescript error TS1005: ':' expected. with Object.assign()

  16. 16

    .getDownloadUrl() gives me error - Android

  17. 17

    using "this" in a fragment gives me an error

  18. 18

    Selecting custom fields of "who" object using SOQL gives no such column error

  19. 19

    MySQL 'AND' gives me error

  20. 20

    using Get for ajax calls gives me 500 error

  21. 21

    It gives me an error at GPS

  22. 22

    Animate UITableViewCell gives me error

  23. 23

    Sorting array gives me error

  24. 24

    For Loop gives Object Required Error

  25. 25

    Firefox gives me dbus error

  26. 26

    Using str.replace gives me a TypeError: 'str' object cannot be interpreted as an integer

  27. 27

    Using 'import * as' in a typescript component gives an error -

  28. 28

    I implemented a Map object in C, but using it gives me a Segmentation Fault

  29. 29

    Yii2- Setting user id to null gives me non-object error

HotTag

Archive