In my Redux action, I have a simple setup:
export interface UpdateUserSettingsPayloadType {
videoInput?: MediaDeviceInfo;
audioInput?: MediaDeviceInfo;
audioOutput?: MediaDeviceInfo;
}
export const updateUserSettings = (
payload: UpdateUserSettingsPayloadType
): UserSettingsActionTypes => {
return {
type: UPDATE_USER_SETTINGS,
payload,
};
};
To correctly map the type from MediaDeviceKind
to my state keys, I am using an object:
// note `type` is type MediaDeviceKind = "audioinput" | "audiooutput" | "videoinput"
const typeMap = {
audioinput: "audioInput",
audiooutput: "audioOutput",
videoinput: "videoInput",
};
dispatch(
updateUserSettings({
[`${typeMap[type]}`]: "a string", // <- incorrect
})
);
An error only occurs when I explicitly declare audioInput
or videoInput
as the key in the updateUserSettings
parameter object:
dispatch(
updateUserSettings({
[`${typeMap[type]}`]: "I should error", // in
audioInput: "i do error",
})
);
Update 1
I have realized that the key is being inferred as [x: string]
:
https://i.sstatic.net/ely7k.png
What I actually need is for it to be inferred as
videoInput | audioInput | audioOutput
This is the expected error I want to see: