fix(useToggle): correct overload typings#2942
Open
dogledogle wants to merge 1 commit into
Open
Conversation
Align useToggle overloads with runtime behavior. * fix incorrect generic overload for no-argument usage * avoid typing one-argument non-boolean usage as the original value type * ensure custom toggle values are only inferred when both values are provided
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Align useToggle overloads with runtime behavior.
[中文版模板 / Chinese template]
🤔 This is a ...
🔗 Related issue link
None
💡 Background and solution
This PR aligns
useToggleoverload typings with its runtime behavior.Previously, the no-argument overload exposed an unnecessary generic type, which could lead to inconsistent typing between the returned state and actions.
For example:
In this case, TypeScript previously allowed actions.set to receive a
string, while state was still typed asboolean. This made the action typings inconsistent with the actual state type.Also, when only
defaultValuewas provided with a non-boolean value, TypeScript inferred the state as the original value type. However, at runtime,reverseValuedefaults to!defaultValue, which means the actual state may becomeboolean.For example:
In this case, TypeScript previously inferred state as
string, but the runtime value could becomefalse.📝 Changelog
useToggleoverload typings to align with runtime behavior.useToggle函数重载类型,使其与运行时行为保持一致。☑️ Self Check before Merge