打字稿函数覆盖高阶函数

typescript function overriding with higher order functions

我对这段代码感到困惑:

/**
 * Lift a function of one argument to a function which accepts and returns values wrapped with the type constructor `F`
 */
export function lift<F extends URIS3>(
  F: Functor3<F>
): <A, B>(f: (a: A) => B) => <U, L>(fa: Type3<F, U, L, A>) => Type3<F, U, L, B>
export function lift<F extends URIS3, U, L>(
  F: Functor3C<F, U, L>
): <A, B>(f: (a: A) => B) => (fa: Type3<F, U, L, A>) => Type3<F, U, L, B>
export function lift<F extends URIS2>(
  F: Functor2<F>
): <A, B>(f: (a: A) => B) => <L>(fa: Type2<F, L, A>) => Type2<F, L, B>
export function lift<F extends URIS2, L>(
  F: Functor2C<F, L>
): <A, B>(f: (a: A) => B) => (fa: Type2<F, L, A>) => Type2<F, L, B>
export function lift<F extends URIS>(F: Functor1<F>): <A, B>(f: (a: A) => B) => (fa: Type<F, A>) => Type<F, B>
export function lift<F>(F: Functor<F>): <A, B>(f: (a: A) => B) => (fa: HKT<F, A>) => HKT<F, B>
/**
 * Lift a function of one argument to a function which accepts and returns values wrapped with the type constructor `F`
 * @function
 */
export function lift<F>(F: Functor<F>): <A, B>(f: (a: A) => B) => (fa: HKT<F, A>) => HKT<F, B> {
  return f => fa => F.map(fa, f)
}

我在 fp=ts repo

中找到了这个

我对此很困惑。每个函数的初始签名看起来都一样 <A, B>(f: (a: A) => B).

typescript 如何确定哪个是正确的调用?

(f: (a: A) => B) 在定义中使用通用类型。

您可以在此处阅读更多内容https://www.typescriptlang.org/docs/handbook/generics.html

我不知道fp-ts,但据推测lift()重载是根据传入的F参数是否为Functor3<>Functor3C<>Functor2<>Functor2C<>。我认为它们之间的差异足以让编译器选择一个。

正如@Sam 在 中提到的那样,您似乎正在查看 lift()return 类型 ,但它并不t 输入选择过载的方程式。 lift() return 另一个函数,您可能会感到困惑?

希望对您有所帮助。