简体   繁体   English

Typescript 中 index.d.ts 的主要用途是什么?

[英]What is the main usage of index.d.ts in Typescript?

I have seen some projects declaring all the types in index.d.ts .我看到一些项目在index.d.ts声明了所有类型。 So that the programmer do not need to explicitly import the type from other files.这样程序员就不需要从其他文件中显式地导入类型。

import { TheType } from './somefile.ts'

Is that the correct usage of index.d.ts file?这是index.d.ts文件的正确用法吗? I'm not able to find anything in the official documentation.我在官方文档中找不到任何内容。

*.d.ts files are used to provide typescript type information about a module that's written in JavaScript, for example underscore / lodash / aws-sdk. *.d.ts文件用于提供有关用 JavaScript 编写的模块的*.d.ts类型信息,例如 underscore / lodash / aws-sdk。

This will allow you to use the javascript modules without the need to convert them to ts without getting any type error on you code.这将允许您使用 javascript 模块,而无需将它们转换为 ts,而不会在您的代码中出现任何类型错误。

for example if you have a folder myAwesomeLib, with index.js and index.d.ts files例如,如果您有一个文件夹 myAwesomeLib,其中包含 index.js 和 index.d.ts 文件

on your code you will be able to import the code with在您的代码上,您将能够导入代码

import { SomeMethod } from './myAwesomeLib';

or或者

import { SomeMethod } from './myAwesomeLib/index';

your typescript will rely on the .d.ts file to find the correct types for the SomeMethod您的打字稿将依赖于.d.ts文件来为SomeMethod找到正确的类型

Edit : More about Declaration files https://basarat.gitbooks.io/typescript/docs/types/ambient/d.ts.html编辑:更多关于声明文件https://basarat.gitbooks.io/typescript/docs/types/ambient/d.ts.html

It's Similar to .h file in C Or you may imagine an interface subsystem in any other language like java or php它类似于 C 中的 .h 文件,或者您可以想象任何其他语言(如 java 或 php)的接口子系统

The file contains function prototype (declaration & typing) of an underlying library该文件包含底层库的函数原型(声明和类型)

for reference on how to generate a .d.ts file from a library.js reefer https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html有关如何从 library.js 冷藏箱生成 .d.ts 文件的参考https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html

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

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