我在我的项目中使用 monocle-ts ( https://github.com/gcanti/monocle-ts ) 库。我有以下代码
import {id, prop} from 'monocle-ts/lib/Lens'
import {pipe} from 'fp-ts/function'
type State = {a: string, b: string}
const stateLens = id<State>()
const aLens = pipe(stateLens, prop('a'))
^ 这段代码工作得很好,类型系统不允许我传递不是“a”或“b”的字符串。但是,如果我尝试以其他方式编写相同的代码:
const aLens = prop('a')(stateLens)
我收到一个错误:函数的Argument of type 'string' is not assignable to parameter of type 'never'
类型定义prop
如下所示:
declare const prop: <A, P extends keyof A>(prop: P) => <S>(sa: Lens<S, A>) => Lens<S, A[P]>
我猜测通过使用管道式打字稿能够以某种方式推断出所有通用参数,而常规prop(...)(...)
调用并非如此