Skip to content

refactor: enhance type annotations for useSessionStorageState#5

Merged
PavelVanecek merged 1 commit into
mainfrom
t
Apr 27, 2026
Merged

refactor: enhance type annotations for useSessionStorageState#5
PavelVanecek merged 1 commit into
mainfrom
t

Conversation

@PavelVanecek
Copy link
Copy Markdown
Contributor

@PavelVanecek PavelVanecek commented Apr 27, 2026

Summary by CodeRabbit

  • Refactor
    • Improved TypeScript type declarations for enhanced code reliability and type safety.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

Walkthrough

The useSessionStorageState hook in TypeScript now includes an explicit return type annotation specifying a tuple of state value and setter function. The internal useState call is also explicitly parameterized to match the declared return type.

Changes

Cohort / File(s) Summary
Type Annotation Refinement
src/hooks/useSessionStorageState.ts
Added explicit return type annotation declaring tuple structure [state: T, setState: (newState: T) => void] and explicitly parameterized useState<T> for type consistency.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: enhancing type annotations for the useSessionStorageState hook by adding explicit return type declarations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch t

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/hooks/useSessionStorageState.ts (2)

29-29: as const is now redundant.

With the explicit return type on line 11, the tuple shape is already enforced; as const adds a readonly modifier the declared type doesn't require, and TS will widen it on return anyway. Safe to drop for clarity:

-  return [state, setState] as const;
+  return [state, setState];
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/hooks/useSessionStorageState.ts` at line 29, The return uses an
unnecessary "as const" which adds a readonly tuple modifier that conflicts with
the explicit return type of useSessionStorageState; remove the "as const" from
the final return (the line returning [state, setState]) so the function simply
returns [state, setState] and relies on the declared return type in
useSessionStorageState, keeping state and setState types consistent.

8-11: Setter type narrows the API and drops functional-updater support.

The declared setter (newState: T) => void is strictly narrower than React's actual Dispatch<SetStateAction<T>> returned by useState. Consumers can no longer call setState(prev => next), even though the underlying useState setter still supports it at runtime. Current call sites in RechartsDevtools.tsx only pass direct values (setSelectedInspectorId('useChartWidth | useChartHeight'), setIsOverlayEnabled(e.target.checked)), so nothing breaks today, but any future consumer wanting the updater form will have to work around this typing.

If the narrowing is intentional (simpler contract), consider documenting it in the JSDoc above. Otherwise, mirror React's type:

♻️ Proposed change to preserve functional-updater support
-import { useState, useEffect } from 'react';
+import { useState, useEffect, type Dispatch, type SetStateAction } from 'react';
@@
 export const useSessionStorageState = <T>(
   id: string,
   initialState: T,
-): [state: T, setState: (newState: T) => void] => {
+): [state: T, setState: Dispatch<SetStateAction<T>>] => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/hooks/useSessionStorageState.ts` around lines 8 - 11, The exported hook
useSessionStorageState narrows the setter type to (newState: T) => void which
drops support for functional-updater usage; change the setter's type to
React.Dispatch<React.SetStateAction<T>> (import React types if needed) and
update the return tuple signature to [state: T, setState:
React.Dispatch<React.SetStateAction<T>>] so callers can pass either a value or
an updater function; if the narrower API was intentional instead, add a JSDoc
note above useSessionStorageState documenting that functional updaters are
intentionally disallowed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/hooks/useSessionStorageState.ts`:
- Line 29: The return uses an unnecessary "as const" which adds a readonly tuple
modifier that conflicts with the explicit return type of useSessionStorageState;
remove the "as const" from the final return (the line returning [state,
setState]) so the function simply returns [state, setState] and relies on the
declared return type in useSessionStorageState, keeping state and setState types
consistent.
- Around line 8-11: The exported hook useSessionStorageState narrows the setter
type to (newState: T) => void which drops support for functional-updater usage;
change the setter's type to React.Dispatch<React.SetStateAction<T>> (import
React types if needed) and update the return tuple signature to [state: T,
setState: React.Dispatch<React.SetStateAction<T>>] so callers can pass either a
value or an updater function; if the narrower API was intentional instead, add a
JSDoc note above useSessionStorageState documenting that functional updaters are
intentionally disallowed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4076fc41-710e-4aee-93e6-ef871c7e007e

📥 Commits

Reviewing files that changed from the base of the PR and between 2e8d78b and 0a4dc1f.

📒 Files selected for processing (1)
  • src/hooks/useSessionStorageState.ts

@PavelVanecek PavelVanecek merged commit 94cb929 into main Apr 27, 2026
2 checks passed
@PavelVanecek PavelVanecek deleted the t branch April 27, 2026 11:22
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.

1 participant