简体   繁体   English

什么是.d.ts文件以及为什么需要它

[英]what is .d.ts file and why it is needed

I'm reviewing a source code for angular-ui-router and there is the angular-ui-router.d.ts file inside api folder with the following content: 我正在查看angular-ui-router的源代码, api文件夹中有angular-ui-router.d.ts文件,内容如下:

declare module ng.ui {

    interface IState {
        name?: string;
        template?: string;
        templateUrl?: any; // string || () => string
        templateProvider?: any; // () => string || IPromise<string>
    }

    interface ITypedState<T> extends IState {
        data?: T;
    }

I've read that is file is TypeScript type definitions. 我已经读过,文件是TypeScript类型定义。 What is it? 它是什么? Why is it needed? 为什么需要它?

It's a declaration file : 这是一个声明文件

When a TypeScript script gets compiled there is an option to generate a declaration file (with the extension .d.ts) that functions as an interface to the components in the compiled JavaScript. 当编译TypeScript脚本时,有一个选项可以生成一个声明文件(扩展名为.d.ts),该文件用作编译JavaScript中组件的接口。 In the process the compiler strips away all function and method bodies and preserves only the signatures of the types that are exported. 在此过程中,编译器会删除所有函数和方法体,并仅保留导出类型的签名。 The resulting declaration file can then be used to describe the exported virtual TypeScript types of a JavaScript library or module when a third-party developer consumes it from TypeScript. 然后,当第三方开发人员从TypeScript使用它时,生成的声明文件可用于描述JavaScript库或模块的导出的虚拟TypeScript类型。

The concept of declaration files is analogous to the concept of header file found in C/C++. 声明文件的概念类似于在C / C ++中找到的头文件的概念。

Here is the definition . 这是定义 The d.ts files describe the shape of 3rd party library, and make the TypeScript compiler know how to deal with this 3rd party code (supply type information to compiler) d.ts文件描述了第三方库的形状,并使TypeScript编译器知道如何处理这个第三方代码(向编译器提供类型信息)

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

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