*. 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 of error on your code.
The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.
Declaration files, if you're not familiar, are just files that describe the shape of an existing JavaScript codebase to TypeScript. By using declaration files (also called . d. ts files), you can avoid misusing libraries and get things like completions in your editor.
The "d.ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.
Rather than rewriting jquery or underscore or whatever in typescript, you can instead write the d.ts file, which contains only the type annotations. Then from your typescript code you get the typescript benefits of static type checking while still using a pure JS library.
This works thanks to TypeScript's constraint of not letting you add the ".ts" extension at the end of the import
statement. Because of that, when you reference some file, let's say, my-module.js
, if there is a my-module.d.ts
next to it, then TypeScript will include its content:
src/
my-module.js
my-module.d.ts
index.ts
my-module.js
const thing = 42;
module.exports = { thing };
my-module.d.ts
export declare const thing: number;
index.ts
import { thing } from "./my-module"; // <- no extension
// runtime implementation of `thing` is taken from ".js"
console.log(thing); // 42
// type declaration of `thing` is taken from ".d.ts"
type TypeOfThing = typeof thing; // number
d
stands for Declaration Files:
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. 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.
The concept of declaration files is analogous to the concept of header file found in C/C++.
declare module arithmetics {
add(left: number, right: number): number;
subtract(left: number, right: number): number;
multiply(left: number, right: number): number;
divide(left: number, right: number): number;
}
Type declaration files can be written by hand for existing JavaScript libraries, as has been done for jQuery and Node.js.
Large collections of declaration files for popular JavaScript libraries are hosted on GitHub in DefinitelyTyped and the Typings Registry. A command-line utility called typings is provided to help search and install declaration files from the repositories.
I could not comment and thus am adding this as an answer.
We had some pain trying to map existing types to a javascript library.
To map a .d.ts
file to its javascript file you need to give the .d.ts
file the same name as the javascript file, keep them in the same folder, and point the code that needs it to the .d.ts
file.
eg: test.js
and test.d.ts
are in the testdir/
folder, then you import it like this in a react component:
import * as Test from "./testdir/test";
The .d.ts
file was exported as a namespace like this:
export as namespace Test;
export interface TestInterface1{}
export class TestClass1{}
Worked example for a specific case:
Let's say you have my-module that you're sharing via npm.
You install it with npm install my-module
You use it thus:
import * as lol from 'my-module';
const a = lol('abc', 'def');
The module's logic is all in index.js
:
module.exports = function(firstString, secondString) {
// your code
return result
}
To add typings, create a file index.d.ts
:
declare module 'my-module' {
export default function anyName(arg1: string, arg2: string): MyResponse;
}
interface MyResponse {
something: number;
anything: number;
}
Like @takeshin said .d stands for declaration file for typescript (.ts).
Few points to be clarified before proceeding to answer this post -
If you are thinking if typescript is just syntactic superset, what benefits does it offer - https://basarat.gitbooks.io/typescript/docs/why-typescript.html#the-typescript-type-system
To Answer this post -
As we discussed, typescript is superset of javascript and needs to be transpiled into javascript. So if a library or third party code is written in typescript, it eventually gets converted to javascript which can be used by javascript project but vice versa does not hold true.
For ex -
If you install javascript library -
npm install --save mylib
and try importing it in typescript code -
import * from "mylib";
you will get error.
"Cannot find module 'mylib'."
As mentioned by @Chris, many libraries like underscore, Jquery are already written in javascript. Rather than re-writing those libraries for typescript projects, an alternate solution was needed.
In order to do this, you can provide type declaration file in javascript library named as *.d.ts, like in above case mylib.d.ts. Declaration file only provides type declarations of functions and variables defined in respective javascript file.
Now when you try -
import * from "mylib";
mylib.d.ts gets imported which acts as an interface between javascript library code and typescript project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With