I am facing an issue with the code below. It seems to work fine without including setCurrentTab
in the return interface useTabsReturnType
, but as soon as I add it, the code stops working and I'm not sure why.
interface useTabsReturnType<T> {
currentTab: T;
setCurrentTab: React.Dispatch<React.SetStateAction<T>>;
}
export function useTabs(defaultValues: number): useTabsReturnType<number>;
//This overload signature is not compatible with its implementation signature.ts(2394)
export function useTabs(defaultValues: string): useTabsReturnType<string>;
export function useTabs(defaultValues?: string | number): useTabsReturnType<string | number> {
const [currentTab, setCurrentTab] = useState(defaultValues || '');
return {
currentTab,
setCurrentTab,
};
}
edit: after following Radu Diță's suggestion I got this error:
Type 'Dispatch<SetStateAction<string | number>>' is not assignable to type 'Dispatch<SetStateAction<string>> | Dispatch<SetStateAction<number>>'.
Type 'Dispatch<SetStateAction<string | number>>' is not assignable to type 'Dispatch<SetStateAction<string>>'.
Type 'SetStateAction<string>' is not assignable to type 'SetStateAction<string | number>'.
Type '(prevState: string) => string' is not assignable to type 'SetStateAction<string | number>'.
Type '(prevState: string) => string' is not assignable to type '(prevState: string | number) => string | number'.
Types of parameters 'prevState' and 'prevState' are incompatible.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.ts(2322)
useTabs.ts(20, 3): The expected type comes from property 'setCurrentTab' which is declared here on type 'useTabsReturnType<number> | useTabsReturnType<string>'
The error came from here:
return {
currentTab,
setCurrentTab,
^^^^^^^^^^^^^
};