我有两个功能如下:
function doForEachLocaleTypeAsync<T, V>(
groups: Record<LocaleType, T[]>,
callback: (locale: LocaleType, items: T[]) => Promise<V>,
) {
return Promise.all(Object.entries(groups).map(([locale, items]) => callback(locale as LocaleType, items)));
}
function groupByLocaleTypes<T, V = T>(
items: T[],
localeKeyExtractor: (item: T) => LocaleType | undefined | null,
valueMapper?: (item: T) => V,
): Record<LocaleType, V[]> {
return items?.reduce(
(acc, item) => {
const key = localeKeyExtractor(item) ?? LocaleType.DE;
const itemValue = valueMapper ? valueMapper(item) : (item as unknown as V);
return {
...acc,
[key]: acc[key].concat(itemValue),
};
},
{
[LocaleType.EN]: [] as V[],
[LocaleType.DE]: [] as V[],
},
);
}
现在我将这两者结合起来使用它:
enum LocaleType {
EN = 'EN',
DE = 'DE',
}
// this is actually imported from Prisma-generated code
type Membership = {
userId: number;
user: { locale: LocaleType }
}
function doEach() {
const activeMemberships: Membership[] = [
{
userId: 1,
user: {
locale: LocaleType.EN,
}
}
];
await doForEachLocaleTypeAsync(
groupByLocaleTypes(
activeMemberships,
(member) => member.user.locale,
(member) => member.userId,
),
(locale, userIds) => console.log(locale, userIds),
);
}
TypeScript 推断
T
的泛型类型 doForEachLocaleTypeAsync
的类型错误,返回其回调的 items
参数类型作为 activeMemberships
的类型,而不是 number[]
。
解决方法是显式提供类型:
// ...
groupByLocaleTypes<Membership, number>(
// ...
知道为什么会出现这种情况吗?
正如评论线程中提到的,我认为这是我的 VSCode 配置的问题 - 如果我重新加载 IDE,通常会显示错误,有时不会。
tsc
或 webpack
没有记录错误。