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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51516394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:56:13  来源:igfitidea点击:

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

typescript

提问by Yong Fei

I have seen some projects declaring all the types in index.d.ts. So that the programmer do not need to explicitly import the type from other files.

我已经看到一些项目在index.d.ts. 这样程序员就不需要从其他文件中显式地导入类型。

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

Is that the correct usage of index.d.tsfile? I'm not able to find anything in the official documentation.

这是index.d.ts文件的正确用法吗?我在官方文档中找不到任何内容。

回答by dege

*.d.tsfiles are used to provide typescript type information about a module that's written in JavaScript, for example underscore / lodash / aws-sdk.

*.d.ts文件用于提供有关用 JavaScript 编写的模块的打字稿类型信息,例如下划线 / 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.tsfile 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