valtio: `readonly` considered as a breaking change?
A readonly
has been introduced in the recent minor 1.2.9 release
Is this considered to be a breaking change? The change makes sense to me and it looks useful, but it in my case, I haven’t explicitly marked props as readonly
. My build breaks in ts strict mode and in order to fix it, I have to mark all props as readonly
(I haven thought much about it before, but to me props are always immutable) where I pass data from useSnapshot
.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 6
- Comments: 34 (19 by maintainers)
Folks, I’ve got a better solution! With #359, we can do Module Augmentation.
https://codesandbox.io/s/react-typescript-forked-qphpi?file=/src/App.tsx:46-125
You don’t need such a complicated type util.
https://tsplay.dev/mZry9m
I hope eventually TypeScript supports it: https://github.com/microsoft/TypeScript/issues/13923
Overall, I think people wouldn’t necessarily want to revert
readonly
change. It’s technically type breaking, so, to mitigate such cases, let’s add a note in readme. Will open a PR.Hi, my suggestion in your case is this (or that):
Does it work? Maybe we should add this in readme.
What about using a wrapper hook for gradual migration?
See: https://tsplay.dev/NrGJlm
fyi for others who found this thread, you can’t add this module augmentation in
decs.d.ts
— it will override the entire module def — can add it in any other regular ts file, thoI prefer to make it the other way around:
useSnapshotLooselyTyped
.I don’t disagree with this. It’s something I really overlooked when reviewing the PR. But, there are opposite opinions from users. So, at least from the library perspective, it should use correct typing.
To use the same deep-readonly method as the library itself (without re-implementing it as suggested above), I just did this and it seems to work as expected:
To me, it looks like exactly the case we want to warn.
Yeah, that’s problem, but it’s only the user who know it. So,
as
should be the right solution.In the next version, we don’t unwrap promises, so the type should be simpler. We may revisit about considering exporting the snapshot type.
fwiw, the change is requested from a user, and it’s correct typing and allows to catch errors at compile time. At this point, there’s no reason to go back.
Good idea! I’ll stay on
1.2.8
and until I have added readonly to my props (or somebody else gives a hint how to fix it 😅)Thanks for reporting. Hm, I thought it’s a proper fix in types, because it’s frozen in JS. But, I overlooked the breaking types in user code. Let me add a note at least in the release page.
Let’s see how this is troublesome for others. If it causes more trouble than benefits, we can consider reverting it.