Dealing with an array of modifications that need to be applied to a specific object:
interface CustomMods {
merge?: MergeModifications,
offset?: OffsetModifications,
split?: SplitModifications,
.....
}
type ModificationsArray = CustomMods[];
Struggling to ensure type safety in this scenario.
const getModification = <V extends CustomMods, K extends keyof V>(mod: V): { modType: K, modSettings: V[K]} => {
const match = Object.keys(mod).find((k) => isNotNullOrUndefined(mod[k as K]));
if (!match){
throw new Error('ERROR')
}
return {
modType: match as K,
modSettings: mod[match as K]
}
}
const {modType, modSettings} = getModification(mod)
switch(modType) {
case 'merge':
// Expected modSettings type to be MergeModsettings here
// but it's currently 'MergeModSettings | OffsetModSettings | SplitModSettings'.
}