ค้นหาเว็บไซต์

javascript - ข้อผิดพลาดในการพิมพ์โดยใช้ Ramda Pipe ใน Typescript


ฉันกำลังใช้วิธี pipe ของ Ramda มันทำงานได้ดีแต่กำลังเกิด ข้อผิดพลาดประเภท บางอย่างกับอาร์กิวเมนต์ตัวแรก flatten

ฉันไม่แน่ใจว่านี่เกี่ยวกับอะไร มีใครช่วยอธิบายปัญหาได้ไหม?

รหัส: https://stackblitz.com/edit/ramda-playground-vcljpy

ข้อผิดพลาด :

ขอโทษสำหรับชื่อเรื่องที่เรียบง่าย

ขอบคุณ

คำตอบ #1

ฉันมักจะพิมพ์ฟังก์ชันแรกใน pipe อย่างชัดเจนโดยทำให้มันชี้ (x: Data[][]) => flatten(x) การกำหนดประเภทที่เหลือควรตามมาได้อย่างราบรื่น

คำตอบ #2

คุณจะต้องระบุประเภทให้กับ R.pipe อย่างชัดเจน:

const mergeData: any = pipe<
  [Data[][]], // arguments supplied to the pipe
  Data[], // result of flatten
  Data[], // result of filter
  Record<string, Data[]>, // result of groupBy
  Data[][], // result of values
  Data[], // result of map(combine)
  RankedData[] // result of last
>(

นี่ใช้ได้กับเวอร์ชันของแพ็กเกจดังต่อไปนี้และเวอร์ชันที่สูงกว่า:

"@types/ramda": "0.27.34",
"ramda": "0.27.1"

นี่คือโค้ดของตัวอย่างที่ทำงานได้ (sandbox):

interface Data {
  name: string;
  val: number;
}

interface RankedData extends Data {
  rank: string;
}

const ranks = {
  a: 'si',
  b: 'dp',
  d: 'en',
  c: 'fr'
};

// merge deep and combine val property values
const combine = mergeWithKey((k, l, r) => (k === 'val' ? l + r : r));

const mergeData: any = pipe<
  [Data[][]],
  Data[],
  Data[],
  Record<string, Data[]>,
  Data[][],
  Data[],
  RankedData[]
>(
  flatten,
  filter((o: Data) => Object.keys(ranks).includes(o.name)),
  groupBy(prop('name')), // group by the name
  values, // convert back to an array of arrays
  map(reduce(combine, {} as Data)), // combine each group to a single object
  map((o) => ({
    ...o,
    rank: ranks[o.name]
  }))
);

บทความที่เกี่ยวข้อง