I am seeking to develop a React hook for retrieving user settings. Here is a simplified example illustrating this:
import React from "react";
interface UserSettings
{
SHOW_TIME_IN_HEADER: boolean;
GRID_SIZE: number;
}
const USER_SETTINGS: UserSettings = {
SHOW_TIME_IN_HEADER: true,
GRID_SIZE: 8,
};
export const useUserSetting = <S extends keyof UserSettings>(...setting: S[]): UserSettings[S][] =>
{
return setting.map(s => USER_SETTINGS[s]);
};
export const Component = () =>
{
const [
showTime, // Should be boolean but it is number | boolean
gridSize // Should be number but it is number | boolean
] = useUserSetting("SHOW_TIME_IN_HEADER", "GRID_SIZE");
return (
<div>
{"..."}
</div>
);
};
I aim for the hook to provide an array containing values of its respective type. In this scenario, both returned values share the type number | boolean
, whereas one should exclusively be of type number
or boolean
.
Is such differentiation achievable? And what would be the appropriate term for it? My primary hurdle at present lies in articulating this issue accurately for research purposes.
If you possess the solution to my dilemma or can articulate the problem more concisely, please do not hesitate to reach out. Thank you.