如何在Angular中使用自定义的Typescript声明文件(.d.ts)?

问题描述 投票:0回答:1

我正在使用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.jsonfilesinclude部分添加到我的typeRoots,但我还没有找到正确的方法。无论我做什么,我仍然得到错误TS2304: Cannot find name 'InvoiceModel'。我该怎么做?

angular typescript typelite
1个回答
2
投票

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

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

我推断你在某种程度上缺少引用声明文件。这是与tripple-slash-directives完成的事情。

您需要做的就是将以下代码添加到您想要使用的位置作为第一行。

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

© www.soinside.com 2019 - 2024. All rights reserved.