Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 在声明文件中将命名空间从外部模块重新导出到全局模块_Typescript_Declaration_Typescript Declarations - Fatal编程技术网

Typescript 在声明文件中将命名空间从外部模块重新导出到全局模块

Typescript 在声明文件中将命名空间从外部模块重新导出到全局模块,typescript,declaration,typescript-declarations,Typescript,Declaration,Typescript Declarations,我在将名称空间从外部模块导出到声明文件中的全局名称空间时遇到了一些问题,如标题中所述。在这里,我将发布一段代码来更好地解释我的问题 //SomeClass.d.ts-由于不相关的原因,我不得不声明这样的类 //externalModule.d.ts //module.d.ts 编辑:也许这可以帮助某人回答这个问题 我发现这样做: \u//script.ts\u /// <reference path="module.d.ts"/> const Module = window.Modu

我在将名称空间从外部模块导出到声明文件中的全局名称空间时遇到了一些问题,如标题中所述。在这里,我将发布一段代码来更好地解释我的问题

//SomeClass.d.ts-由于不相关的原因,我不得不声明这样的类

//externalModule.d.ts

//module.d.ts

编辑:也许这可以帮助某人回答这个问题

我发现这样做:

\u//script.ts\u

/// <reference path="module.d.ts"/>
const Module = window.Module;
var SomeClass = new window.Module.SomeClass();
但是,我必须将
var SomeClass=new window.Module.SomeClass()
替换为
var SomeClass(给出一种类型的any),以避免不必要的模块.SomeClass初始化。

这是一个我最好避免的糟糕的解决方法。

执行以下操作不起作用,因为
const
s和名称空间是不同的:

// module.d.ts
declare global {
    interface Window {
        Module: module;
    }
    const Module: typeof module;
    type Module = typeof module;
}
但是,以下各项应起作用:

//module.d.ts
import module = require("./externalModule");
export = module
export as namespace Module
declare global {
    interface Window {
        Module: typeof module;
    }
}

请注意,如果您可以修改externalModule.d.ts,则只需在此处添加
导出为命名空间模块
声明。

只需测试它,但它会给出错误TS2503:找不到命名空间“模块”。错误TS2304:找不到名称“模块”。不过,我可以修改externalModule.d.ts,但我必须将其保留为本地模块。@giulianoclacchioni复制并粘贴snafu,抱歉。现在试试。
/// <reference path="module.d.ts"/>
const Module = window.Module;
var SomeClass = new window.Module.SomeClass();
var someObject: typeof SomeClass;
// module.d.ts
declare global {
    interface Window {
        Module: module;
    }
    const Module: typeof module;
    type Module = typeof module;
}
//module.d.ts
import module = require("./externalModule");
export = module
export as namespace Module
declare global {
    interface Window {
        Module: typeof module;
    }
}