TypeScript Object assign gives me an error property assign does not exist on type ObjectConstructor

0 votes

API data:

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

My code has a photos variable and a method called 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
    )
}

For the next call, I get back 

{"photos":[{"id":2,"title":"photo_2_title"}]}

Can someone help me with why jsfiddle.net/ca46hLw9 doesn't work?

May 30, 2022 in TypeSript by Logan
• 2,140 points
3,010 views

1 answer to this question.

0 votes

For TypeScript 2.1 and higher, you can do it like this instead

let obj = { x: 1, y: "string" };
var newObj = {...obj, z: 3, y: 4}; // { x: number, y: number, z: number }
answered May 31, 2022 by Nina
• 3,060 points

Related Questions In TypeSript

0 votes
1 answer

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

You can use type assertion, like this: (<any>Object).as ...READ MORE

answered Aug 3, 2022 in TypeSript by Abhinaya
• 1,160 points
6,722 views
0 votes
1 answer

TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'

If you want a key/value data structure ...READ MORE

answered Jun 7, 2022 in TypeSript by Nina
• 3,060 points
21,446 views
0 votes
1 answer

Redux + TypeScript -> Error: A case reducer on a non-draftable value must not return undefined redux

reducers: { updateMode(state: SliceState, ...READ MORE

answered Jun 8, 2024 in TypeSript by abhi

edited Mar 5, 2025 11,205 views
0 votes
1 answer

Now I know what 'pick' does. But I want to do the opposite and remove values from an object.

You can use the keyword Omit for ...READ MORE

answered May 27, 2022 in TypeSript by Nina
• 3,060 points
1,384 views
0 votes
1 answer

Cannot access web3 object with typescript and ethereum

You still need to instantiate it first. ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,370 points
3,535 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

To create a sequential zoom-in and zoom-out ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,130 points
2,355 views
0 votes
1 answer
0 votes
1 answer

Can not bind to 'formgroup' since it is not a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,690 points
23,375 views
0 votes
1 answer

Typescript Errors: How do I ignore the error "property does not exist on value of type"

to ignore it globally, in your tsconfig.json, ...READ MORE

answered Jul 26, 2023 in TypeSript by john

edited Mar 5, 2025 13,078 views
0 votes
1 answer

Typescript: Property "XXX" does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes

Double check the newly added object types. ...READ MORE

answered Jun 8, 2022 in TypeSript by Nina
• 3,060 points
50,567 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP