About "*.d.ts" in TypeScript
I learned the following from having to painstakingly map a JavaScript project using .d.ts
files.
Using a .d.ts
file to map JavaScript requires that you name your .d.ts
files the same as you name your .js
files. Each .js
file needs to be kept inline (kept in the same directory) with the .d.ts
file that has the same name. Point the JS/TS code that needs the types from the .d.ts
files to the .d.ts
files.
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{}
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
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;
}
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.