newbie
newbie

Reputation: 600

Typing Error using Ramda Pipe in Typescript

I am using Ramda's pipe method. It runs fine but it is giving some type error on first argument flatten.

I am not sure what it is about. Can anyone please explain the issue?

Code: https://stackblitz.com/edit/ramda-playground-vcljpy

Error:

enter image description here

Sorry for naive title

Thanks

Upvotes: 3

Views: 4141

Answers (2)

Ori Drori
Ori Drori

Reputation: 191986

You'll need to explicitly supply the types to 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
>(

This works with the following packages' versions and above:

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

This is the code of the working example (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]
  }))
);

Upvotes: 6

AirbusDriver
AirbusDriver

Reputation: 116

I usually explicitly type the first function in the pipe by making it pointed (x: Data[][]) => flatten(x) The rest of the typings should follow nicely

Upvotes: 7

Related Questions