如何在typescript中添加声明到现有对象(键入augmentation)

时间:2016-05-23 18:27:04

标签: typescript typescript-typings

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

但是lodash打字是针对旧版本而没有那些功能。

AllowClose

如何将这些函数的声明添加到import _ = require('lodash'); 对象?

1 个答案:

答案 0 :(得分:0)

如果您检查您的打字文件,您可以看到可以扩展的基本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)

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