Search code examples
typescriptramda.jspointfree

RamdaJs with typescript, avoid errors when point free


const getColumnsBySection = R.pipe(
    R.filter(c => c.section != null),
    R.groupBy(c => c.section)
  );

When using point free with RamdaJs as in this function. I get typescript errors

 Type 'Dictionary<unknown>' is missing the following properties from type 'readonly unknown[]': length, concat, join, slice, and 18 more.
TS2339: Property 'section' does not exist on type 'unknown'

How are you suppose to use point free functions without getting typescript errors like these?


Solution

  • You can either cast the function to the types you expect, or you can lean more on the ramda library with a construction like this instead:

    const { anyPass, complement, pipe, propSatisfies, filter, groupBy, prop, isNil, isEmpty } = R
    
    const isBlank = anyPass([isNil, isEmpty]);
    
    const isPresent = complement(isBlank);
    
    const getColumnsBySection = pipe(
      filter(propSatisfies(isPresent, 'section')),
      groupBy(prop('section'))
    );
    
    const samples = [
      {section: 'trees', name: 'birch'}, 
      {section: 'vines', name: 'grape'},
      {section: 'trees', name: 'cedar'},
      {section: 'vines', name: 'ivy'},
    ];
    
    const results = getColumnsBySection(samples);
    console.log(results);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>