generic typescript routine to update a complex object using Type request property?

Viewed 15

I'm working on a redux toolkit implementation. Each input on a CRUD maintenance page needs to dispatch the user input onchange to a reducer handler function which updates an Appointment object in global state. The request object that gets passed to the reducer function needs to look something like this:

interface AppointmentUpdateRequest {
    level1: string;
    level2: string;
    level3: string;
    value: object;
    type: Type;
}

updateAppointment: (
    state: Draft<typeof initialState>,
    action: PayloadAction<AppointmentUpdateRequest>
) => {
    if(action.payload.level1 && !action.payload.level2 && !action.payload.level3)
        state.appointment[action.level1] = action.payload.value;
    
    else if(action.payload.level1 && action.payload.level2 && !action.payload.level3)
        state.appointment[action.level1][action.level2] = action.payload.value;
    
    else if(action.payload.level1 && action.payload.level2 && action.payload.level3)
        state.appointment[action.level1][action.level2][action.level3] = action.payload.value;
}

But the twist here is that I need to set the value based on the type specified in the request object. For example:

state.appointment[action.level1] = action.payload.value as action.payload.value.type;

Do you know if this style of dynamic type casting is possible? The first issue that I'm encountering is that vscode returns the following error for the type property in the AppointmentUpdateRequest object in its current definition:

Cannot find name 'Type'

0 Answers
Related