首页 > 解决方案 > 如何在 Angular 中使用自定义 Typescript 声明文件 (.d.ts)?

问题描述

我正在使用 TypeLite 从 Web API 项目中的一些 C# POCO 生成 Typescript 接口。这会生成一个文件,如:

// TypeLite.Net4.d.ts
declare namespace MyNamespace.Models {
    interface InvoiceModel {
        billingPeriodId: number;
        createdDate: Date;
        invoiceId: number;
        invoiceNumber: number;
        organizationId: number;
        paidDate: Date;
        total: number;
    }
}

我想在我的 Angular 项目中使用这个文件,但我似乎找不到如何做的很好的解释。我尝试了在网上找到的各种方法,包括将其添加到我tsconfig.jsonfilesincludetypeRoots部分中,但我还没有找到正确的方法。无论我做什么,我仍然得到错误TS2304: Cannot find name 'InvoiceModel'。我该怎么做?

标签: angulartypescripttypelite

解决方案


由于您尚未发布使用生成的打字稿声明文件的片段,并考虑到您的部分问题

我尝试了我在网上找到的各种东西,包括将其添加到文件、包含或 typeRoots 部分中的 >my tsconfig.json”

我推断您在某种程度上缺少对声明文件的引用。这是用三斜杠指令完成的。

您需要做的就是将下面的代码添加到您想将其用作第一行的任何位置。

/// reference path=".../path/to/file" />


推荐阅读