Apologies for the title confusion, let me clarify my problem.
I am utilizing a React state management library where my application state is structured as follows:
type ApplicationState = {
loading: boolean;
data: string[];
colors: number[];
alerts: any;
error: string;
}
const state: ApplicationState = {
loading: false,
data: [],
colors: [],
alerts: {},
error: "some error"
}
I have a setter function in place to ensure that the key corresponds to a valid key in ApplicationState, and that the value matches one of the corresponding types in the state.
const mySetter: <K extends keyof ApplicationState>(key: K, value: ApplicationState[K]) => ApplicationState = (key, value) => {
return {
...state,
[key]: value,
}
}
My query pertains to how I can restrict the key (K) to only certain specified types within the state (such as limiting it to changing loading, alerts, and error)? Currently, the setup allows usage of any key (K) from ApplicationState.
Thank you for your assistance!