Ramda 的 LensProp 类型定义

Y M*_*Y M 5 typescript ramda.js typescript-typings

当使用 RamdalensProp时:

R.lensProp('x')
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)
Run Code Online (Sandbox Code Playgroud)

Ale*_*yne 8

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

这是来自文档的修改后的示例,它与打字稿配合得很好:

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}
Run Code Online (Sandbox Code Playgroud)

操场