打字稿:当名称空间和class在库文件中具有相同名称时导入名称空间

Typescript: Import namespace when namespace and class have the same name in a library file

我在从声明命名空间和同名 class 的文件导入命名空间时遇到问题。我可以访问 class 但不能访问命名空间。

docs 开始,我认为从导出合并命名空间和 class 的库中导入将为您提供来自两个声明的属性。但是,我只能从 class 获取属性。

Namespaces are flexible enough to also merge with other types of declarations. To do so, the namespace declaration must follow the declaration it will merge with. The resulting declaration has properties of both declaration types. TypeScript uses this capability to model some of the patterns in JavaScript as well as other programming languages.

这是我的场景,

库文件:

class GoldenLayout {
}

namespace GoldenLayout {
    export interface Config {
    }
}

在我的项目中,我尝试使用 Config 界面。我正在尝试以这种方式使用它,

import * as GoldenLayout from 'golden-layout';

const INITIAL_LAYOUT = GoldenLayout.Config = {
};

但是,我得到一个错误

Property 'Config' does not exist on type 'typeof GoldenLayout'.

我可以访问 class GolderLayour 中的属性和方法,但我不知道如何访问命名空间。

作为参考,我正在尝试在我的 Angular 8 应用程序中使用此 library

我认为你的问题是你没有定义配置而是双重分配。试试这个:

const INITIAL_LAYOUT: GoldenLayout.Config = { };