I am trying to make well typed function for getting only distinct values of object's property in array. So it works like that
type Employee = { work: WorkEnum; name: string };
const arg1: Employee[] = [{ name: "kornad", work: WorkEnum.Doctor}, { name: "Adam", work: WorkEnum.FrontEndDeveloper}]
const result1: WorkEnum[] = getDistinct(arg1, 'work')
const result1: string[] = getDistinct(arg1, 'name')
so function needs to detect possible keys for seconds argument (that I've managed to do) and type of the value (I don't know how to do this one)
Here is my function
type ArrayObject<V> = {
[key: string]: V;
};
function getDistinct<V, T extends ArrayObject<V>>(
data: T[],
property: keyof T
): V[] {
const allValues = data.reduce((values: V[], current) => {
if (current[property]) {
values.push(current[property]);
}
return values;
}, []);
return [...new Set(allValues)];
}
const arrayOfData: { xxx: string; qwe: string | number }[] = [
{ xxx: 'asd', qwe: 43 },
{ xxx: 'asd', qwe: 'dsadas' },
];
const res = getDistinct(arrayOfData, 'xxx'); // res: unknown[], why not string[] ??????????
So Typescript cannot figure it out that res should be string[] instead of this I am getting here unknown[]. How can I fix it?