如何在打字稿中输入lodash流函数? [英] How should the lodash flow function be typed in typescript?

查看:100
本文介绍了如何在打字稿中输入lodash流函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lodash.flow 结合了两个或更多功能。

lodash.flow combines two or more functions.

lodash.flow( double,addTwo)将返回一个将加倍的函数并添加两个。该函数应该如何在typescript中输入(当前定义只返回Function)?

lodash.flow(double, addTwo) would return a function the would double and add two. How should this function be typed in typescript (the current definition just returns Function)?

声明函数流< In,Intermediate,Out>(f1 :(a1:In)=>中间体,f2:(a1:中间体)=> Out):( a1:In)=> Out 适用于两个函数,第一个函数有一个输入参数。我不确定如何在所有情况下扩展定义。

declare function flow<In, Intermediate, Out>(f1: (a1: In) => Intermediate, f2: (a1: Intermediate) => Out): (a1: In)=> Out works for two functions with the first having one input argument. I'm not sure how to extend the definition to work in all cases.

如果像这样调用,我的尝试可以支持多个函数:

My attempt can support mulitple functions if called like this:

lodash.flow(f1,lodash.flow(f2,f3))

但我正在寻找

lodash.flow(f1,f2,f3)

推荐答案

我认为你不能写出这个定义。

I don't believe you can write that definition.

如果我们看一下 lodash类型声明文件他们不试图表达这种关系。

If we look at the lodash type declaration file they don't try to express that relationship.

interface LoDashStatic {
    flow<TResult extends Function>(...funcs: Function[]): TResult;
}

但仅凭这一点并不足以打折这种可能性。作者可能只是忽略了一些东西,所以让我们继续思考它。

But that alone isn't reason enough to discount the possibility. The authors may have just overlooked something, so let's keep thinking about it.

单个函数链之间的关系是你可以表达的。你已经在上面的例子中这样做了。您可以为多个参数长度创建相同构思的手动版本,但这是因为您正在设置链的长度已知并且您可以授予单个类型信息的情况。

The relationship between an individual chain of functions is something you can represent. You've done so in your example above. You could create manual versions of that same idea for several lengths of parameters but that's because you're setting up a situation where the length of the chain is known and you can grant individual type information.

如果我们要处理可变长度参数的情况,我们必须将参数视为集合。因为所有变量必须具有单个(尽管可能是参数化的)类型,所以此集合对象也必须如此。但是,各种功能的类型不对齐。 (param:A)=> B (param:B)=>的类型不同C 并且不能存储在同一个良好类型的容器中(除了联合类型,但那些也不会缩放)。

If we are to handle the case of variable length parameters, we must treat the parameters as a Collection. Because all variables must have a single (though possibly parameterized) type so too must this collection object. However, the types of the various functions do not align. (param:A) => B is not the same type as (param:B) => C and cannot be stored in the same well typed container (barring union types but those won't scale either).

在想要在这样的参数列表中保留类型信息的情况下,通常在两个参数上定义组合函数并将其应用于多个函数。例如,这就是在承诺中保留类型信息的方式。为此,您仍然需要拼出每个单独的参数。它只是使它到最后你有正确的输出类型。也就是说,在大多数情况下,这就是你想要的,所以这一切都很好。

In situations where you want to retain type information on a list of parameters like this you usually define the composition function on two parameters and apply it across a number of functions. This is how type information is retained in promises, for example. For this to work you still need to have each individual parameter spelled out. It just makes it so by the end you've got the proper output type. That said, in most cases this is what you want so it's all good.

如果lodash是用一种良好的函数式语言编写的,那么这个流函数可能就不存在了。我想它会被写成一个管道组合物。

If lodash were written in a well typed functional language, that flow function probably wouldn't exist. I imagine it would instead have been written as a piped composition object.

更新:当我说管道组合物时,我的意思是什么?这样的事情可能是:

UPDATE: What do I mean when I say a "piped composition object"? Something like this, perhaps:

class FunctionComposer<T,V> {
    constructor(protected func: (param: T) => V) { }

    public compose<X>(newFunc: (param:V) => X) {
        return new FunctionComposer((x: T) => newFunc(this.func(x)));
    }
    public out() {
        return this.func;
    }
}

let composedFunc = new FunctionComposer((x: number) => x * 2)
    .compose(x => x.toString())
    .out();

// composedFunc has type (param:number) => string

这篇关于如何在打字稿中输入lodash流函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆