Как перевести функцию в typeScript?

проблемы при переводе в ts.
1. m.length - m Объект имеет тип "Неизвестный"
2. const res = data?.map - Свойство "map" не существует в типе "Compare"
export type Compare = {
    channelid: number,  
    childrenHash: Child[]
}

export type Child = {
  id: number, hash: string
}

export const comparsion = (data: Compare) => {
    const res = data?.map((n: { childrenHash: any[] }) => {
    const ids = Object.values(n.childrenHash.reduce((acc, m) => ((acc[m.hash] ??= []).push(m.id), acc), {})).filter(m => m.length > 1);
    return {
      ...n,
      childrenHash: ids.length ? ids : null,
    };
  })
  return res
}


на js работает.
  • Вопрос задан
  • 99 просмотров
Решения вопроса 1
@HealSpirit
type THash = {
  id: number;
  hash: string;
};
type TChannel = {
  channelid: number;
  childrenHash: THash[];
};
type TData = TChannel[];
type THashAcc = Record<THash["hash"], THash["id"][]>;

export const comparsion = (data: TData) =>
  data.map((n) => {
    const ids = Object.values(
      n.childrenHash.reduce<THashAcc>((acc, m) => ((acc[m.hash] ??= []).push(m.id), acc), {})
    ).filter((m) => m.length > 1);

    return {
      ...n,
      childrenHash: ids.length ? ids : null,
    };
  });
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы