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 - Fatal编程技术网

声明仅部分加载的内部模块的函数-typescript编译器错误

声明仅部分加载的内部模块的函数-typescript编译器错误,typescript,Typescript,我使用一个内部模块(“AllianceModule”),它被拆分为两个文件。 第一个在页面启动期间加载。第二个仅在需要时加载。 我将加载脚本过程从创建新的HTMLScript元素更改为使用$.ajax加载第二个脚本。 我这样做是为了在加载内部模块的第二部分时可以使用回调方法。此回调加载一个方法(称为“entryPoint”),该方法是第二个脚本的一部分 VisualStudio可以很好地编译所有内容,并且程序运行良好。 从命令行运行ts编译器 tsc References.ts --out ma

我使用一个内部模块(“AllianceModule”),它被拆分为两个文件。 第一个在页面启动期间加载。第二个仅在需要时加载。 我将加载脚本过程从创建新的HTMLScript元素更改为使用$.ajax加载第二个脚本。 我这样做是为了在加载内部模块的第二部分时可以使用回调方法。此回调加载一个方法(称为“entryPoint”),该方法是第二个脚本的一部分

VisualStudio可以很好地编译所有内容,并且程序运行良好。 从命令行运行ts编译器

tsc References.ts --out main.js
其中references.ts包含页面启动期间所需的所有文件,则会出现错误:

C:\sources\TSProject\Onload.ts(137126):错误TS2094:类型为“typeof AllianceModule”的值上不存在属性“entryPoint”

“entryPoint”是第二个模块的导出“静态”函数,它不是从第一个模块内部调用的

我试着加上

declare function entryPoint(allianceId: number, _parent: JQuery): void;
但编译器会抛出相同的错误。创建一个接口并定义接口类型的变量也不起作用(但我怀疑我用这种方式尝试时确实出错)

如何声明该方法,以便不再抛出错误(除了“强制转换”到任何然后调用该方法之外)

Onload.ts:

function onLoadDoThis()
{
    //some code...
    $("#aButton").click(function () { Scripts.scriptsAdmin.loadAndRun(3, 3, './Alliances.js', true, () => {(AllianceModule).entryPoint(0,null); } ); });
    //more code
}
$(document).ready(onLoadDoThis);
AlliancesBase.ts文件包含

module AllianceModule {
    //a lot of code always needed and thus loaded during startup
}
module AllianceModule {
    //some alliances stuff only needed when the user does some specific actions

    export function entryPoint(allianceId: number,_parent : JQuery) {

        if (allianceId) {
            console.log('EinzelAllianz');
            (new AllianceDetails(allianceId)).allianceDetails(_parent);
        }
        else {
            console.log("Alle Allianzen");
            runTemplate();
        }
    }
}
文件Alliances.ts包含

module AllianceModule {
    //a lot of code always needed and thus loaded during startup
}
module AllianceModule {
    //some alliances stuff only needed when the user does some specific actions

    export function entryPoint(allianceId: number,_parent : JQuery) {

        if (allianceId) {
            console.log('EinzelAllianz');
            (new AllianceDetails(allianceId)).allianceDetails(_parent);
        }
        else {
            console.log("Alle Allianzen");
            runTemplate();
        }
    }
}
我正在编译References.ts,其中只包含对OnLoad.ts和AlliancesBase.ts的引用

附言:

第一个答案建议将declare函数放在模块内部。这没有帮助,可能是因为我正在从模块外部调用该方法(单击按钮的事件)。我在AlliancesBase.ts中直接在声明之后创建了一个转发方法:

declare function entryPoint(allianceId: number, _parent: JQuery): void;
export function entryPoint2(allianceId: number, _parent: JQuery) {
    AllianceModule.entryPoint(allianceId, _parent);
}
这也没有帮助,因为我一直将entryPoint作为导出函数调用,而tsc编译器在编译References.ts文件时不知道该导出函数

declare function entryPoint(allianceId: number, _parent: JQuery): void;

那是包裹在模块里的吗?如果不是,那么它声明的是一个全局
入口点
函数,而不是AllianceModule中的一个方法。将它包装在
模块联盟module{…}
中,您可以在其中声明它可以这样做。

可能会发布一些实际的代码?很难从文本描述中猜出哪里出了问题。我在问题中添加了实际代码。对不起,这不起作用。如果我这样声明函数,那么它只能从模块内部看到。我需要从外部访问该方法。我在模块内部创建了一个转发方法——这是可行的,类型检查至少是有保证的。但这不是一个优雅的解决方案。。。如果没有更好的解决办法,我会接受你的回答