如何在打字稿中向现有对象添加声明(打字增强) [英] How to add declare to existing object (typing augmentation) in typescript
本文介绍了如何在打字稿中向现有对象添加声明(打字增强)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
上一个版本的 lodash 现在有一些新功能.例如:https://lodash.com/docs#nth.
Last version of lodash now has a few new functions. For example: https://lodash.com/docs#nth.
但是 lodash 类型适用于旧版本并且没有这些功能.
But lodash typings are for old version and hasn't those functions.
import _ = require('lodash');
如何将这些函数的声明添加到 _
对象中?
How I can add declaration of those functions to _
object?
推荐答案
如果你检查你的 Typings 文件,你可以看到你可以扩展的基本 LoDashStatic 接口:
If you check your typings file you can see the basic LoDashStatic interface that you can extend:
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)
对于简单的情况,这应该足够了.您可能希望将上述声明放入一个模块中,然后导出新的 _ 值.
That should be enough for simple cases. You might want to put the above declarations to a module and just export your new _ value.
这篇关于如何在打字稿中向现有对象添加声明(打字增强)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文