Javascript 如何将mixin与lodash和typescript结合使用,并提供链接支持
当我试图用我的自定义混音扩展Lodash时,我在将Lodash与typescript一起使用时遇到了问题 我失败的尝试: 假设我使用mixin向lodash添加了一个新函数,如下所示: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
//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”上不存在。
}
import代码>而不是标准从“lodash”导入代码>
可能有帮助。您正在寻找模块扩充。通过重新定义接口并添加额外的方法,可以扩展现有模块中的接口。在这种情况下,对于静态使用,您应该增加
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。