Trying to help someone with React. React doesn't like it when data mutates. React prefers you to make copies rather than mutate structs in place.

The person I'm helping is using TypeScript classes. Why wouldn't they? But then we discover something weird. If you want to make a copy of a TypeScript object, the two ways to do this are:
- structuredClone
- spread. You could say like, { a:4, ...otherObject } to create a copy of otherObject with "a" changed.

Both of these destroy class identity!

Is there a good way, in TypeScript, to cleanly, typesafely, make a copy of an object while changing exactly one field, while using classes rather than type = and literal objects? Is there a trick for spreading an object without it turning into something other than an object? We found an okay looking lens library, but the lens library seemed to want you to work with plain objects not classes.

0

If you have a fediverse account, you can quote this note from your own instance. Search https://mastodon.social/users/mcc/statuses/115498311392737220 on your instance and quote it. (Note that quoting is not supported in Mastodon.)