Skip to content

fix(useToggle): correct overload typings#2942

Open
dogledogle wants to merge 1 commit into
alibaba:masterfrom
dogledogle:fix/use-toggle-overload-types
Open

fix(useToggle): correct overload typings#2942
dogledogle wants to merge 1 commit into
alibaba:masterfrom
dogledogle:fix/use-toggle-overload-types

Conversation

@dogledogle

Copy link
Copy Markdown

Align useToggle overloads with runtime behavior.

  • no-argument usage returns boolean
  • one-argument usage only supports boolean
  • custom toggle values are inferred only when both defaultValue and reverseValue are provided

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

None

💡 Background and solution

This PR aligns useToggle overload 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:

const [state, actions] = useToggle<string>();
actions.set('open');

In this case, TypeScript previously allowed actions.set to receive a string, while state was still typed as boolean. This made the action typings inconsistent with the actual state type.

Also, when only defaultValue was provided with a non-boolean value, TypeScript inferred the state as the original value type. However, at runtime, reverseValue defaults to !defaultValue, which means the actual state may become boolean.

For example:

const [state, actions] = useToggle('open');
actions.toggle();

In this case, TypeScript previously inferred state as string, but the runtime value could become false.

📝 Changelog

Language Changelog
🇺🇸 English Fix useToggle overload typings to align with runtime behavior.
🇨🇳 Chinese 修复 useToggle 函数重载类型,使其与运行时行为保持一致。

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

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
@CLAassistant

CLAassistant commented Jun 3, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@pkg-pr-new

pkg-pr-new Bot commented Jun 6, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/ahooks@2942
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2942

commit: 9a239af

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants