Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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.

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

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

like image 839
Yong Fei Avatar asked Jul 25 '18 10:07

Yong Fei


People also ask

What is the use of D ts file in TypeScript?

d. ts is the type definition files that allow to use existing JavaScript code in TypeScript. declare function sum(a: number, b: number): number; From now on, we can use the function in TypeScript without any compile errors.

What is D ts file in react?

d. ts files. Those files are called declaration files and provide typescript type information about APIs of a package.

Where does TypeScript look for D ts files?

The . d. ts file is usually placed adjacent to the . ts file, it is providing the typings for.


2 Answers

*.d.ts files are used to provide typescript type information about a module that's written in JavaScript, for example 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.

for example if you have a folder myAwesomeLib, with index.js and index.d.ts files

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

Edit: More about Declaration files https://basarat.gitbooks.io/typescript/docs/types/ambient/d.ts.html

like image 146
dege Avatar answered Sep 27 '22 21:09

dege


It's Similar to .h file in C Or you may imagine an interface subsystem in any other language like java or 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

like image 28
Subrat Kumar Palhar Avatar answered Sep 27 '22 22:09

Subrat Kumar Palhar