如何从 Typescript 中的数组中获取不同的值
How to get distinct values from array in Typescript
我正在尝试制作类型良好的函数,以仅获取数组中对象 属性 的不同值。所以它是这样工作的
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')
所以函数需要检测秒参数的可能键(我已经设法做到了)和值的类型(我不知道如何做到这一点)
这是我的函数
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[] ??????????
所以 Typescript 无法弄清楚 res
应该是 string[]
而不是我在这里 unknown[]
。我该如何解决?
据我所知,ArrayObject
定义和函数 return 类型不正确。
这会起作用:
function getDistinct<T, K extends keyof T>(data: T[], property: K): T[K][] {
const allValues = data.reduce((values: T[K][], 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 res1 = getDistinct(arrayOfData, 'xxx'); // string[]
const res2 = getDistinct(arrayOfData, 'qwe'); // (string | number)[]
重要的部分是将 属性 定义为 keyof T
,并将 return 类型定义为与 属性 关联的类型(T[K]
),或者在本例中,该类型的数组 (T[K][]
).
我正在尝试制作类型良好的函数,以仅获取数组中对象 属性 的不同值。所以它是这样工作的
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')
所以函数需要检测秒参数的可能键(我已经设法做到了)和值的类型(我不知道如何做到这一点)
这是我的函数
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[] ??????????
所以 Typescript 无法弄清楚 res
应该是 string[]
而不是我在这里 unknown[]
。我该如何解决?
据我所知,ArrayObject
定义和函数 return 类型不正确。
这会起作用:
function getDistinct<T, K extends keyof T>(data: T[], property: K): T[K][] {
const allValues = data.reduce((values: T[K][], 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 res1 = getDistinct(arrayOfData, 'xxx'); // string[]
const res2 = getDistinct(arrayOfData, 'qwe'); // (string | number)[]
重要的部分是将 属性 定义为 keyof T
,并将 return 类型定义为与 属性 关联的类型(T[K]
),或者在本例中,该类型的数组 (T[K][]
).