Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify ReadOnly values?

I have this value fetchOptions: Readonly<HttpFetchOptionsWithPath> and I'd like to overwrite one of its properties. I've tried the following:

((fetchOptions as Writable<HttpFetchOptionsWithPath>).headers as Writable<any>) = {
    'new-value': '123',
    ...(fetchOptions.headers || {}),
  };

but still get an TypeError: Cannot assign to read only property 'headers' of object '#<Request>' error.

The js code that gets executed looks like this:

fetchOptions.headers = __assign({ 'new-value': '123' }, (fetchOptions.headers || {}));

Any ideas what I'm doing wrong here?

like image 725
user8811409 Avatar asked Jun 27 '26 00:06

user8811409


1 Answers

You can't modify the read-only values.. what you can do is cloning these object into new instance and continue using the new instance ...

Object.assign(newVariable, JSON.parse(JSON.stringify(oldVariable)));

This also will do the job:

const newVariable={...oldVariable, propertyToModify:value}
like image 141
Hussein Akar Avatar answered Jun 28 '26 16:06

Hussein Akar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!