如何在 Typescript 中扩展全局命名函数

How to extend global named functions in Typescript

我定义了一个全局命名函数,例如:

function foo() { 
  foo._cache = {}; 
}

如何将 属性 扩展为 foo,例如 _cache

看来下面的代码没有效果。

interface foo {
    _cache: any;
}

How do I extend a property to foo like _cache.

函数 foo 与接口 foo 没有关联,因为它们位于完全不同的声明空间 (more on those)。

如果你想让 foo 成为一个 function 也有一个 cache 属性 你必须分两步完成,例如:

const foo: {
    (): void;
    _cache: any;
} = function () {
} as any;
foo._cache = {};

终于在官方文档中找到了解决方法:https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Declaration%20Merging.md#merging-namespaces-with-classes