Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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
Javascript 如何将mixin与lodash和typescript结合使用,并提供链接支持_Javascript_Typescript_Lodash_Mixins_Chaining - Fatal编程技术网

Javascript 如何将mixin与lodash和typescript结合使用,并提供链接支持

Javascript 如何将mixin与lodash和typescript结合使用,并提供链接支持,javascript,typescript,lodash,mixins,chaining,Javascript,Typescript,Lodash,Mixins,Chaining,当我试图用我的自定义混音扩展Lodash时,我在将Lodash与typescript一起使用时遇到了问题 我失败的尝试: 假设我使用mixin向lodash添加了一个新函数,如下所示: //lodashMixins.ts import * as _ from "lodash"; _.mixin({ swap }); function swap(array: Array<any>, index1: number, index2: number) { let temp

当我试图用我的自定义混音扩展Lodash时,我在将Lodash与typescript一起使用时遇到了问题

我失败的尝试:

假设我使用mixin向lodash添加了一个新函数,如下所示:

//lodashMixins.ts

import * as _ from "lodash";

_.mixin({
  swap
});

function swap(array: Array<any>, index1: number, index2: number) {
    let temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    return array;
}
并使用新的混合材料,如下所示:

//lodashMixins.ts

import * as _ from "lodash";

interface LodashExtended extends _.LoDashStatic {
  swap(array: Array<any>, index1: number, index2: number): Array<any>;
}

_.mixin({
  swap
});

function swap(array: Array<any>, index1: number, index2: number) {
    let temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    return array;
}

export default _ as LodashExtended;
//otherFile.ts

import _ from "./lodashMixins";

function someFn(){
    var array = [1,2,3,4]
    _.swap(array, 1, 2);
}
//otherFile.ts

import _ from "./lodashMixins";

function someFn(){
    var cantDothis = _.chain([1,2,3,4]).map(x=>x*x).swap(1,2).value();
    //[ts] Property 'swap' does not exist on type 'LoDashExplicitWrapper<number[]>'.

    var neitherThis = _([1,2,3,4]).map(x=>x*x).swap(1,2).value();
    //[ts] Property 'swap' does not exist on type 'LoDashImplicitWrapper<number[]>'.
}
现在这是可行的,但是有两个问题:

  • 首先,新的
    swap
    函数不使用lodash的链接语法(既不使用显式链接也不使用隐式链接)
  • 也就是说,如果我做了以下任何一项,打字脚本就会生气:

    //lodashMixins.ts
    
    import * as _ from "lodash";
    
    interface LodashExtended extends _.LoDashStatic {
      swap(array: Array<any>, index1: number, index2: number): Array<any>;
    }
    
    _.mixin({
      swap
    });
    
    function swap(array: Array<any>, index1: number, index2: number) {
        let temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
        return array;
    }
    
    export default _ as LodashExtended;
    
    //otherFile.ts
    
    import _ from "./lodashMixins";
    
    function someFn(){
        var array = [1,2,3,4]
        _.swap(array, 1, 2);
    }
    
    //otherFile.ts
    
    import _ from "./lodashMixins";
    
    function someFn(){
        var cantDothis = _.chain([1,2,3,4]).map(x=>x*x).swap(1,2).value();
        //[ts] Property 'swap' does not exist on type 'LoDashExplicitWrapper<number[]>'.
    
        var neitherThis = _([1,2,3,4]).map(x=>x*x).swap(1,2).value();
        //[ts] Property 'swap' does not exist on type 'LoDashImplicitWrapper<number[]>'.
    }
    
    //otherFile.ts
    从“/lodashMixins”导入;
    函数someFn(){
    var cantDothis=uu.chain([1,2,3,4]).map(x=>x*x).swap(1,2).value();
    //[ts]属性“swap”在类型“LoDashExplicitWrapper”上不存在。
    var neitherThis=u([1,2,3,4]).map(x=>x*x).swap(1,2).value();
    //[ts]属性“swap”在类型“LoDashImplicitWrapper”上不存在。
    }
    
  • 第二,我必须从“/lodashMixins”导入丑陋的
    import而不是标准
    从“lodash”导入
  • 请有人想出一个优雅的解决方案,在链接时提供typescript类型支持,而无任何代码气味或丑陋。。 谢谢。:)


    可能有帮助。

    您正在寻找模块扩充。通过重新定义接口并添加额外的方法,可以扩展现有模块中的接口。在这种情况下,对于静态使用,您应该增加
    LoDashStatic
    ,对于链使用,您应该增加
    LoDashExplicitWrapper
    。使用该模块时,您可以先导入
    lodash
    ,然后导入包含
    swap
    的模块,以了解其副作用(将方法添加到
    lodash
    的副作用)

    //swap.ts
    从“lodash”中导入*as uu;
    声明模块“lodash”{
    界面LoDashStatic{
    交换(数组:数组,index1:number,index2:number):TValue[];
    }
    接口LoDashExplicitWrapper{
    交换(index1:number,index2:number):LoDashExplicitWrapper;
    }
    }
    _.米辛({
    交换
    });
    
    函数交换(array:array

    完美答案。非常感谢。:)非常有用!我还必须添加接口
    LoDashImplicitWrapper
    ,以便TS正确键入
    。[1,2,3]).swap(0,2).value()
    。TS v3.2.2,Lodash v4.17.11。