When attempting to utilize Ramda with TypeScript, I encountered a type problem, particularly when using compose. Here are the required dependencies:
"devDependencies": {
"@types/ramda": "^0.25.21",
"typescript": "^2.8.1",
},
"dependencies": {
"ramda": "^0.25.0"
},
Below is the code snippet in question:
import { curry, compose, reject, isNil, tail } from 'ramda'
const arrayToObj = curry((props: string[], array: any[]): Record<string, any> => {
const len = props.length
const result: Record<string, any> = {}
if (len > array.length) throw "TODO: arrayToObj not matching size"
for (let i = 0; i < len; ++i) result[props[i]] = array[i]
return result
})
// Other functions and compositions here...
An issue arises within the compose function where tail is used, which results in a type error stating List: List: ReadonlyArray<T>
being incompatible with x: string
(from the definition of compose
). Removing tail resolves this error, but attempts to cast the output of matchRegExp
to string[]
did not work. This discrepancy is confusing, especially since it does not cause issues when tail is omitted prior to calling the matchToObject
function.
In an effort to troubleshoot, I attempted to place the compose
before tail
, incorporating the Array.from
method, only to encounter an error indicating that
from is not defined in type 'ArrayConstructor'
. This further adds to the confusion.