JavaScript > TypeScript > Advanced TypeScript > Declaration files
Creating and Using TypeScript Declaration Files
This example demonstrates how to create and use TypeScript declaration files (.d.ts) to provide type information for JavaScript libraries or modules.
What are Declaration Files?
Declaration files are TypeScript files that contain only type declarations. They tell the TypeScript compiler about the shape of existing JavaScript code. This allows you to use JavaScript libraries in your TypeScript projects and get type checking, autocompletion, and other benefits without having to rewrite the JavaScript code in TypeScript.
Creating a Declaration File (my-library.d.ts)
This code defines a declaration file for a hypothetical JavaScript library called 'my-library'. It declares a module with the same name. Inside the module, it declares a function `myAwesomeFunction`, an interface `MyOptions`, and a class `MyClass`. The declarations specify the types of the function's arguments and return value, as well as the properties of the interface and the methods of the class.
// my-library.d.ts
declare module 'my-library' {
export function myAwesomeFunction(name: string): string;
export interface MyOptions {
option1: boolean;
option2: number;
}
export class MyClass {
constructor(options: MyOptions);
doSomething(): void;
}
}
Explanation of the Declaration File
Using the Declaration File in a TypeScript Project
This TypeScript code imports the declarations from 'my-library'. Thanks to the declaration file, TypeScript knows the types of `myAwesomeFunction`, `MyOptions`, and `MyClass`, and provides type checking and autocompletion. If you make a mistake, like passing a number to `myAwesomeFunction`, the TypeScript compiler will flag it as an error.
// my-typescript-file.ts
import { myAwesomeFunction, MyOptions, MyClass } from 'my-library';
const result = myAwesomeFunction('TypeScript');
console.log(result);
const options: MyOptions = {
option1: true,
option2: 123
};
const myInstance = new MyClass(options);
myInstance.doSomething();
Real-Life Use Case
Imagine you're using a popular JavaScript charting library that doesn't have TypeScript definitions. You can create a declaration file to describe the library's API, allowing you to use it safely and effectively in your TypeScript project. This avoids having to use `any` types and losing type safety.
Best Practices
When to use them
Declaration files are used when:
Alternatives
Pros
Cons
FAQ
-
What happens if I don't have a declaration file for a JavaScript library?
TypeScript will not have any type information about the library, and you'll likely have to use the `any` type, which defeats the purpose of using TypeScript for type safety. -
How does TypeScript find declaration files?
TypeScript looks for declaration files in the same directory as the JavaScript file, and in directories specified in the `typeRoots` and `types` compiler options in the `tsconfig.json` file.