-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SharedValueType type #1462
Fix SharedValueType type #1462
Conversation
Im pretty sure the current type should work without Could you please provide reproducing project for this one? I wonder perhaps there are some differences in our ts configs or so. You check for boolean only here, but actually if it's the case then |
Hey @terrysahaidak, if you update
and run Edit: my tsc version is 4.0.2 |
So you're fixing the case when you specify explicitly type for shared value? |
Let me check tomorrow why I don't have this error in my gallery-toolkit project. |
Here is a repro with TS playground |
Hey @terrysahaidak, any update on this ? |
@Andarius I'm having the same TS errors and your types work for me. 👍 |
At first glance, I wasn't happy with that solution because it effectively hardcodes booleans and if in the future we'd want to add another union type we'll have to hardcode it there as well. I read some discussions about this in the TS repo and seems like this is a limitation of distributing over unions so we can't do much about this. @wcandillon, maybe you can suggest something to make it less hacky? |
As @terrysahaidak mentioned, I cannot reproduce the original issue. Could it be related to the typeScript version or could you provide me with a sample file where I could reproduce the issue? |
@wcandillon have you tried the TS playground link @Andarius sent? Which TS version are you on? I can reproduce the issue in my project on v4.1.2 I find it rather weird that it doesn't abstract |
@mrousavy, as mentioned by @jakub-gonet, it's a typescript limitation (see this issue for instance: microsoft/TypeScript#41333) |
@mrousavy Thanks a lot! Sorry if this was already explained in another thread but why not do:
|
So |
I'm not sure what is the use case of RawSharedValue |
I have no idea too, but that would make it simpler here |
@jakub-gonet @terrysahaidak green light to delete RawSharedValue? |
This is more a question what cannot be a shared value. Perhaps @Szymon20000 or @karol-bisztyga can answer this question. If we can pass just anything then yeah we can remove this constraint, otherwise type safety makes here no sense since we don't type safe anything which may lead to runtime errors. |
In general, we can't store anything in the shared value (e.g. |
@jakub-gonet unfortunately that's not so easy, as we have I think the simple approach @wcandillon suggested is our only option, and for stuff that's not supported (e.g. Map) we should just check those in debug builds and throw errors. |
That's unfortunate. I was afraid we'll relax this type too much but I feel convinced, if @terrysahaidak doesn't have any further objections I think we can change it. |
Should we check for undefined then? We can assign null, but can we assign undefined? because this constraint is not only for the thing we can pass to useSharedValue, but also for the thing we can assign to But I still don't understand why we (me and @wcandillon) don't have such errors in our projects. I guess something is wrong with our ts configs. |
@terrysahaidak I think that we would have the same issue it's just that we don't have the exact same use case (boolean type that gets refined via if statement and then re-assigned in that if branch). |
@terrysahaidak why should we check for undefined? |
one more argument for removing RawSharedValue is that it doesn't contain null which is geniuly helpful |
I pushed a new version with |
I'm hitting a similar issue by assigning an enum to a shared value. Removing |
Sorry for the delay, I think we can merge that one. @Andarius, could you resolve conflicts? |
## Description `SharedValue` type can cause errors when using a boolean as type for `useSharedValue`. The following code will raise an error because `sharedBool` becomes of type `Animated.SharedValue<false> | Animated.SharedValue<true>` (because `SharedValue<T extends SharedValueType>` extends the type `boolean` as `true | false`) instead of `Animated.SharedValue<boolean>` ``` const sharedBool = useSharedValue<boolean>(false) if(sharedBool.value) sharedBool.value = false ```
Description
SharedValue
type can cause errors when using a boolean as type foruseSharedValue
.The following code will raise an error because
sharedBool
becomes of typeAnimated.SharedValue<false> | Animated.SharedValue<true>
(becauseSharedValue<T extends SharedValueType>
extends the typeboolean
astrue | false
) instead ofAnimated.SharedValue<boolean>
Changes
Updated
react-native-reanimated-tests.tsx
andreact-native-reanimated.d.ts
Before
Raise an error:
Type 'false' is not assignable to type 'true'.
After
Does not raise the error
Checklist