我有一组 typescript 文件,它们被编译成 *.d.ts
文件,但它一直给我带来错误:
systems.debug.d.ts(280,41):错误 TS2094:“Systems”类型的值上不存在属性“Models”。
声明如下:
declare module Systems.Services.Stuff {
class DefaultHubService {
public Send(entry: Systems.Models.IEntry, successCallback: Function, failureCallback: Function): void;
}
}
这发生在第 280 行(systems.debug.d.ts)
,它正在努力引用的是:
declare module Systems.Models {
interface IEntry {
Version: string;
Content: KnockoutObservableString;
DateCreated: KnockoutObservableDate;
CreatorAccountId: KnockoutObservableString;
}
}
这是第239行(systems.debug.d.ts)
,所以我觉得很奇怪,不知道如何解决这个问题。由于接口(interface)包含在服务之前,据我所知,除非 Typescript 有一些怪癖,否则它应该可以工作。谁能解释一下这个错误?
==编辑==
只是提供一些额外的信息,以帮助诊断问题...
实际的实现如下所示:
// Systems/Services/Stuff/default-hub-service.ts
module Systems.Services.Stuff {
export class DefaultHubService {
public Send(entry: Models.IEntry, successCallback: Function, failureCallback: Function): void
{
// Do something here
}
}
}
// Systems/Models/ientry.ts
module Systems.Models {
export interface IEntry {
Version: string;
Content: KnockoutObservableString;
DateCreated: KnockoutObservableDate;
CreatorAccountId: KnockoutObservableString;
}
}
各个文件中没有引用,因为我有一个根级引用文件,其中包含要使用的所有外部引用,并且由于它首先包含在编译参数中,所以它提供了所有外部依赖项(即,knockout.d.ts) 、jquery.d.ts 等)。
我使用的是 Typescript 0.9.0.1,并通过直接从构建脚本调用 tsc.exe 进行编译。最后,JavaScript 输出文件创建时没有错误,因此我有一个可以正常编译的 systems.debug.js
,但是使用声明标志,它只有 d.ts 文件会引发错误。
最佳答案
当我尝试你的代码时,我没有收到错误 - 这是我正在使用的完整示例:
declare class KnockoutObservableString {}
declare class KnockoutObservableDate {}
declare module Systems.Models {
interface IEntry {
Version: string;
Content: KnockoutObservableString;
DateCreated: KnockoutObservableDate;
CreatorAccountId: KnockoutObservableString;
}
}
declare module Systems.Services.Stuff {
class DefaultHubService {
public Send(entry: Systems.Models.IEntry, successCallback: Function, failureCallback: Function): void;
}
}
var x = new Systems.Services.Stuff.DefaultHubService();
var y: Systems.Models.IEntry;
我tried this in the TypeScript Playground以及带有 0.9.0.1 TypeScript Visual Studio 扩展的 Visual Studio 中。在 Visual Studio 中,我将环境声明分离到名为 systems.debug.d.ts
的文件中,并将该声明的使用放入 app.ts
中。
关于Typescript 定义文件内部模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868113/