我开始在我的ASP.NET MVC项目中使用TypeScript。我的档案看起来像这样:
Project
|-ts
| |- Common
| | |-AppSettings.ts
| |-Components
| | |-DeleteModal.ts
AppSetting.ts 包含一个具有代表全局应用程序常数的静态属性的类:
namespace Ls.Common {
export class AppSettings {
public static readonly DeleteMethod = 'DELETE';
}
}
deletemodal.ts 使用ls.common.appsettings.deletemethod值作为用于制作Ajax请求的类型。
$.ajax({
type: Ls.Common.AppSettings.DeleteMethod,
url: this.config.deleteUrl,
async: true
})
.done((data) => {
// Display result
});
Visual Studio在DeleteModal.ts中显示以下错误:
属性'appSettings'不存在于类型的'type of common'。
使用GULP构建TS文件有效,并创建预期的输出。如何使Visual Studio了解其他TS文件中的类型(在同一项目内)?
看来,Visual Studio在分析中没有包含文件。由于Gulp起作用,因此Gulp可能使用正确的tsconfig.json
,而Visual Studio则不使用。
.csproj
中包含Typescript配置通常很常见,并且默认情况下,配置将覆盖tsconfig.json
中的内容。
请检查您是否在上面提到的不同位置具有不同的配置。csproj
中的一个看起来像
<PropertyGroup>
<TypeScriptModuleKind>amd</TypeScriptModuleKind>
<TypeScriptNoImplicitAny>True</TypeScriptNoImplicitAny>
....
</PropertyGroup>
在 deletemodal.ts 中添加一个问题解决了问题:
/// <reference path="../common/appsettings.ts" />