refactor: enhance type annotations for useSessionStorageState#5
Conversation
WalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/hooks/useSessionStorageState.ts (2)
29-29:as constis now redundant.With the explicit return type on line 11, the tuple shape is already enforced;
as constadds areadonlymodifier 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) => voidis strictly narrower than React's actualDispatch<SetStateAction<T>>returned byuseState. Consumers can no longer callsetState(prev => next), even though the underlyinguseStatesetter still supports it at runtime. Current call sites inRechartsDevtools.tsxonly 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
📒 Files selected for processing (1)
src/hooks/useSessionStorageState.ts
Summary by CodeRabbit