首页 > 解决方案 > Ramda 的 LensProp 类型定义

问题描述

当使用 Ramda 的时候lensProp喜欢:

R.lensProp('x')

我收到此错误:

Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)

标签: typescripttypescript-typingsramda.js

解决方案


看起来您需要传入您希望lensProp操作的类型,以便它知道使用时要返回的类型。您可以通过传入泛型参数来做到这一点。

这是与 typescript 配合得很好的文档中的修改示例:

import R from 'ramda'

interface Point {
    x: number
    y: number
}

const xLens = R.lensProp<Point>('x');

R.view(xLens, {x: 1, y: 2});            //=> 1
R.set(xLens, 4, {x: 1, y: 2});          //=> {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2});  //=> {x: -1, y: 2}

操场


推荐阅读