如何在TypeScript中使用lodash.mixin [英] How to use lodash.mixin in TypeScript

查看:150
本文介绍了如何在TypeScript中使用lodash.mixin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队正在评估将我们的某些文件从JavaScript切换到TypeScript的过程,并且我们在代码中广泛使用了一些自定义的mixin方法.通过进行一些基本测试,似乎我们可以按照常规使用_.mixin来创建mixins,但在没有编译错误的情况下我们无法对其进行引用.当然,我们可以将这些引用放入定义文件中,但我通常不希望对其进行修改.

My team is evaluating switching some of our files to TypeScript from JavaScript, and we make extensive use of some custom mixin methods in our code. From doing some basic tests, it seems that while we can use _.mixin to create mixins as per the norm, we cannot reference them without getting a compilation error. Of course, we could put these references in the definition file, but I usually prefer not to modify that.

有什么方法可以做我们正在寻找的东西,还是我们不走运?

Is there any way to do what we're looking for, or are we out of luck?

推荐答案

就目前而言,似乎我想要的东西一无所获.相反,我必须修改lodash.d.ts文件以包含所需的定义,类似于以下内容:

For now, it looks like what I want isn't available without any pain. Instead, I have to modify the lodash.d.ts file to include the definitions that I want, similar to the following:

declare module _ {
    // Default methods declared here...

    //*************************************************************************
    // START OF MIXINS, THESE ARE NOT PART OF LODASH ITSELF BUT CREATED BY US!
    //*************************************************************************

    interface LoDashStatic {
        isNonEmptyString: (str: string) => boolean;
        isEmptyString: (str: string) => boolean;
        isEmptyArray: (a: any[]) => boolean;
        isNonEmptyArray: (a: any[]) => boolean;
        isNullOrEmptyString: (str: string) => boolean;
        isNullOrUndefined: (val: any) => boolean;
        isNullOrEmpty(value: any[]): boolean;
        isNullOrEmpty(value: _.Dictionary<any>): boolean;
        isNullOrEmpty(value: string): boolean;
        isNullOrEmpty(value: any): boolean;
    }

    //*************************************************************************
    // END OF MIXINS
    //*************************************************************************

    // Default types declared here...
}

我讨厌修改默认文件,但这似乎弊端较小.

I hate modifying the default files, but it seemed the lesser of the evils.

这篇关于如何在TypeScript中使用lodash.mixin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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