Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types 成员函数的类型引用与同一静态版本之间的差异_Types_Typescript - Fatal编程技术网

Types 成员函数的类型引用与同一静态版本之间的差异

Types 成员函数的类型引用与同一静态版本之间的差异,types,typescript,Types,Typescript,在下面的代码中,有两种方法可以实现相同的功能,即使用静态方法和使用成员函数。它们都使用泛型,但成员函数只是将所有工作委托给静态版本。问题是编译器执行不同的类型推断 我问了一个非常相似的问题,但我的解释很糟糕 类频道{ 静态flatMapImpl(自:通道,投影仪:(t:t)=>通道){ var ch=新通道(); 返回ch; } 公共平面图(投影仪:(数据:Y)=>频道){ 返回channel.flatmapmpl(此为投影仪); } 公共合并2(…目标:频道[]){ var ch=new ch

在下面的代码中,有两种方法可以实现相同的功能,即使用静态方法和使用成员函数。它们都使用泛型,但成员函数只是将所有工作委托给静态版本。问题是编译器执行不同的类型推断

我问了一个非常相似的问题,但我的解释很糟糕

类频道{
静态flatMapImpl(自:通道,投影仪:(t:t)=>通道){
var ch=新通道();
返回ch;
}
公共平面图(投影仪:(数据:Y)=>频道){
返回channel.flatmapmpl(此为投影仪);
}
公共合并2(…目标:频道[]){
var ch=new channel()。命名为('merge');
var r=channel.flatmapmpl(ch,x=>x);//很好!
返回r;
}
公共合并(…目标:频道[]){
var ch=new channel()。命名为('merge');

var结果=ch.flatMap(x=>x);//您的代码没有多大意义……1.方法
flatmappl
不使用它接收到的任何参数,那么有什么意义呢?2.当您调用
flatMap
flatmappl
时,您传递
x=>x
,即使这两个方法都没有定义为接收这样的方法1,要点是显示类型ISISION,不是实现2不理解这一点
x=>x
函数(x){return x;}
,您将其传递给
flatMap
flatmapmpl
,但它们都不例外,它们所期望的是
(Y)=>通道
class channel<S,Y> {

static flatMapImpl<T,R>(self: channel<any, T>, projector:(t:T) => channel<T,R>) {
    var ch = new channel<T, R>();

    return ch;  
}

public flatMap<R>(projector: (data: Y) => channel<Y, R>) {
    return channel.flatMapImpl(this, projector);
}

public merge2(...targets: channel<any, Y>[]) {
    var ch = new channel<void, channel<any, Y>>().named('merge');
    var r = channel.flatMapImpl(ch, x => x); // fine !

    return r;
}

public merge(...targets: channel<any, Y>[]) {
    var ch = new channel<void, channel<any, Y>>().named('merge');
    var result = ch.flatMap(x => x); // <-- compilation error

    return result;
}
}