I have created this specific function
function extractSingleValue<T, TElem, K extends keyof T>(obj: T, name: K): TElem {
const source = obj[name];
if (source.length !== 1) {
throw Error(`There should be exactly one ${name} associated`);
}
return _.first(source);
}
This function is designed to retrieve the unique value from an array that is stored within the specified property of the original object. However, I am unsure about how to correctly type this function using the following syntax:
function extractSingleValue<T, TElem, K extends keyof T>(obj: T, name: K): TElem
where T[K] extends TElem[]
{
const source = obj[name];
if (source.length !== 1) {
throw Error(`There should be exactly one ${name} associated`);
}
return _.first(source);
}
Is there a way to achieve this desired typing for the function?