将管道参数实现为数组时推断类型丢失

问题描述

我正在尝试键入一个管道函数,其中管道中的值作为数组传递,但我有一个问题,即除了第一个和最后一个类型之外的所有类型都被推断为“未知”。

这是一个初始示例,其中管道值是单独的参数,所有推断的类型都符合预期(派生自 RxJS 中的管道方法):

    type UnaryFunction<P,R> = (singleParameter:P)=>R
        
    declare function p(): null;
    declare function p<A>(op1: UnaryFunction<null,A>): A;
    declare function p<A,B>(op1: UnaryFunction<null,A>,op2: UnaryFunction<A,B>): B;
    declare function p<A,B,C>(op1: UnaryFunction<null,B>,op3: UnaryFunction<B,C>): C;
     
    p();
    p(()=>1);
    p(()=>1,x=>'a');
    p(()=>1,x=>'a',xIsString=>true);  // hover over "xIsString" to see that the type is correctly "string"

这里是相同的示例,转换后将参数传递到数组中。但是,最后一个示例中推断的类型 B 现在是“未知”。

declare function q(elements: []): null;
declare function q<A>(elements: [UnaryFunction<null,A>]): A;
declare function q<A,B>(elements: [UnaryFunction<null,UnaryFunction<A,B>]): B;
declare function q<A,C>(elements: [UnaryFunction<null,UnaryFunction<B,C>]): C;
 
q([]);
q([ ()=>1 ]);
q([ ()=>1,x=>'a']);
q([ ()=>1,xIsUnknown=>true]); // hover over "xIsUnknown" to see that the type is "unknown"

Playground link

这个解决方案应该有效,还是有其他解决方法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)