【问题标题】:How to add declare to existing object (typing augmentation) in typescript如何在打字稿中向现有对象添加声明(键入增强)
【发布时间】:2016-09-20 17:57:34
【问题描述】:

最新版本的 lodash 现在有一些新功能。例如:https://lodash.com/docs#nth

但是 lodash 类型是针对旧版本的,没有这些功能。

import _ = require('lodash');

如何将这些函数的声明添加到_ 对象?

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    如果您检查您的类型文件,您可以看到可以扩展的基本 LoDashStatic 接口:

    import old = require('lodash')
    
    interface LodashExt extends old.LoDashStatic {
        nth(n: Array<any>, i: number) : LodashExt
        // . . .
    }
    
    var _ = <LodashExt>old
    
    _.add(1, 2)
    
    _.nth(['a', 'b', 'c', 'd'], 2)
    

    这对于简单的情况应该足够了。您可能希望将上述声明放到一个模块中,然后导出您的新 _ 值。

    【讨论】:

    • 现在我正在这样做。但我想要没有新变量。
    猜你喜欢
    • 1970-01-01
    • 2020-05-31
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2020-01-08
    • 2021-01-20
    相关资源
    最近更新 更多