declare in TypeScript: Why It's Needed and How to Use It

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why does TypeScript need the declare keyword?

\n

Imagine you are building a house, but some rooms already exist — they were built by someone else (for example, a JavaScript library). You can't go in and rebuild the walls, but you need to know their layout to place the furniture correctly. This is exactly the role the declare keyword plays in TypeScript.

\n\n

If you've ever worked with popular libraries (jQuery, Lodash, D3.js), you've likely encountered .d.ts files — these are the "layouts" created using declare. In this article, we'll break down what declare is, why it's needed, and how to use it in practice. Get ready — there will be lots of code and little fluff.

\n\n

What is declare in TypeScript?

\n

declare is a keyword that tells the TypeScript compiler: "Hey, this thing (variable, function, class, or module) exists, but it's defined somewhere else. Just take my word for it and let me use it in the code."

\n\n

It's important to understand: declare does not generate any JavaScript code. It is purely an informational construct for the compiler. If you write declare const x: number = 5, after compilation x will disappear — it won't be in the final file.

\n\n

When is it needed?

\n
    \n
  • Working with global variables — for example, window, document, or variables added via CDN.
  • \n
  • Typing JavaScript libraries — when the library is written in plain JS, but you want to use it with TypeScript.
  • \n
  • Creating types for configuration files — CSS modules, images, JSON files.
  • \n
\n\n

declare syntax: basic examples

\n\n

1. Declaring variables

\n

Suppose you have a global variable VERSION that is defined in an HTML file via <script>. TypeScript knows nothing about it, so you write:

\n
declare const VERSION: string;\n\nconsole.log(VERSION); // OK, TypeScript knows it's a string\n
\n

After compilation, declare const VERSION: string will disappear, but console.log(VERSION) will remain.

\n\n

2. Declaring functions

\n

Imagine you have a global analytics function trackEvent:

\n
declare function trackEvent(eventName: string, properties?: Record<string, any>): void;\n\n// Usage\ntrackEvent("button_click", { buttonId: "submit" });\n
\n

The compiler won't check the implementation — it just knows the signature.

\n\n

3. Declaring classes

\n

A library provides a Moment class from the global namespace:

\n
declare class Moment {\n    constructor(date: string);\n    format(pattern: string): string;\n}\n\n// Usage\nconst now = new Moment("2024-01-01");\nconsole.log(now.format("YYYY-MM-DD"));\n
\n\n

4. Declaring modules

\n

This is the most common scenario. For example, you have CSS modules or JSON files:

\n
// styles.d.ts\ndeclare module "*.css" {\n    const styles: Record<string, string>;\n    export default styles;\n}\n\n// images.d.ts\ndeclare module "*.png" {\n    const src: string;\n    export default src;\n}\n
\n

Now you can import CSS and images without TypeScript errors.

\n\n

declare and .d.ts files

\n

Files with the .d.ts extension (type declarations) are the "habitat" of the declare keyword. They contain only declarations and are not compiled into JavaScript. They usually reside in a types/ folder or are shipped with the library (e.g., @types/lodash).

\n\n

Example: creating types for jQuery

\n

Blogs

Book Recommendations