简体   繁体   English

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

[英]How do I use a custom Typescript Declaration file (.d.ts) from within Angular?

I am using TypeLite to generate Typescript interfaces from some C# POCOs in a Web API project. 我正在使用TypeLite从Web API项目中的某些C#POCO生成Typescript接口。 This generates a file like: 生成的文件如下:

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

I want to use this file in my Angular project, but I can't seem to find a good explanation of how to do it. 我想在我的Angular项目中使用此文件,但似乎找不到如何做的很好的解释。 I tried various things I've found online including adding it to my tsconfig.json in the files , include , or typeRoots sections, but I haven't found the right way to do it yet. 我尝试了各种在线发现的事情,包括将它们添加到tsconfig.jsonfilesincludetypeRoots部分中,但是我还没有找到正确的方法。 No matter what I do, I still get the error TS2304: Cannot find name 'InvoiceModel' . 无论我做什么,我仍然会收到错误TS2304: Cannot find name 'InvoiceModel' How do I do it? 我该怎么做?

Since you haven't posted the snippet from where you are using your generated typescript declaration file, and considering to the part of your question 由于您尚未从使用生成的打字稿声明文件的位置发布代码段,并考虑了问题的一部分

I tried various things I've found online including adding it to >my tsconfig.json in the files, include, or typeRoots sections" 我尝试了各种在线发现的内容,包括将其添加到文件,包含或typeRoots部分的> tsts.json中。“

I am inferring that you are somehow missing to make a reference to the declaration file. 我推断您以某种方式缺少对声明文件的引用。 That's something which done with tripple-slash-directives . 这是使用tripple-slash-directives完成的

All you need to do is to add the below code wherever you want to use that as the first line. 您需要做的就是在要用作第一行的位置添加以下代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM