TypeScript Declaration Files
Declaration files act as an interpreter between TypeScript and JavaScript libraries, telling TypeScript what features a JavaScript library exposes, what types its parameters are, and what types its return values are.
Start with a problem
Suppose you are writing a project in TypeScript and need to import a third-party JavaScript library (such as jQuery); you might write code like this:
$('#foo');
// 或
jQuery('#foo');
This code is perfectly fine in a pure JavaScript project, but in a TypeScript file it will produce an error directly:
jQuery('#foo');
// index.ts(1,1): error TS2304: Cannot find name 'jQuery'.
Why the error? Because TypeScript does not know what $ and jQuery are.
The core feature of TypeScript is type checking — at compile time it needs to know the type of every variable and every function. But jQuery is a pure JavaScript library with no type information, so TypeScript naturally cannot understand it.
Quick fix: the declare keyword
The simplest way is to use the declare keyword to manually tell TypeScript: "this variable exists, and its type is like this":
declare var jQuery: (selector: string) => any;
jQuery('#foo');
This line means: declare a variable jQuery, which is a function that takes a string parameter and can return any type (any).
Now TypeScript will not report an error, because the compiler already knows what type jQuery is.
declare Types declared with keywords only take effect during compilation; they are completely removed from the compiled JavaScript code and do not affect runtime behavior.
The compiled JavaScript code of the above example is:
jQuery('#foo');
As you can see, the declare statement has disappeared, leaving only the actual calling code.
But declare can only solve a temporary problem in a single file. If a library has many methods and many classes, hand-writing declare in every file is obviously impractical. This is where declaration files come in.
Declaration files: a permanent solution
A declaration file puts all declare declarations together in a single file, which any TypeScript file in the project can reference.
File naming conventions
Declaration files uniformly use the .d.ts suffix, where d stands for declaration. For example:
runoob.d.ts
Basic Syntax
The syntax for declaring a module is as follows:
declare module Module_Name {
}
In a TypeScript file, reference a declaration file with a triple-slash directive:
///
The triple-slash directive is syntax specific to TypeScript, used to tell the compiler to include a specified declaration file during compilation.
Declaration files for many popular third-party libraries (such as jQuery and Lodash) are already maintained by the community and stored in the DefinitelyTyped project. You only need to install the corresponding @types/xxx package via npm to use it, with no need to write it manually.
Complete example: creating a declaration file from scratch
The following complete example demonstrates the entire process of creating and using a declaration file.
The whole process involves the following files:
| file | Purpose |
|---|---|
| CalcThirdPartyJsLib.js | A third-party JavaScript library (pure JS, no type information) |
| Calc.d.ts | A declaration file (written manually, describing the library's types) |
| CalcTest.ts | TypeScript business code (references the declaration file, calls the library) |
| CalcTest.js | Compiled output (JS files after tsc compilation) |
| runoob.html | The final page running in the browser |
Step 1: Create a third-party JavaScript library
Suppose we have a third-party library that provides cumulative summation. It uses the namespace Runoob to organize code and avoid variable name conflicts:
Code for the CalcThirdPartyJsLib.js file:
// File path: CalcThirdPartyJsLib.js
// This is a simulated third-party JavaScript library
// Declare the Runoob namespace variable (create an empty object if it does not exist)
var
Runoob
;
// Use an immediately invoked function expression (IIFE) to wrap the code, preventing internal variables from leaking into the global scope
(
function
(
Runoob
)
{
// The Calc constructor, used to create calculator objects
var
Calc
=
(
function
(
)
{
function
Calc
(
)
{
// No initialization parameters are needed currently; keep the constructor for future extension
}
}
)
// The doSum method: calculate the sum of all integers from 0 to limit
// limit (required): the upper bound of the summation, inclusive of the value itself
// Example: when limit=10, calculate 0+1+2+...+10 = 55
Calc.
prototype
.
doSum
=
function
(
limit
)
{
var
sum
=
0
;
for
(
var
i
=
0
;
i
<=
limit
;
i
++
)
{
sum
=
sum
i
;
}
return
sum
;
}
// Attach the Calc constructor to the Runoob namespace
// This way, the outside can create instances via new Runoob.Calc()
Runoob.
Calc
=
Calc
;
return
Calc
;
}
)
(
Runoob
||
(
Runoob
=
{
}
)
)
;
// Library internal self-test code
var
test
=
new
Runoob.
Calc
(
)
;
This library uses a common pattern: the immediately invoked function expression (IIFE). In simple terms, it wraps code in a function and runs it immediately, so variables defined inside the function do not leak outside, avoiding name conflicts with other code.
Step 2: Write the declaration file
Now we have the JS library, but it has no type information. We need to create a declaration file that describes only the "shape" of the library — what classes and methods it has, and what types its parameters and return values are — without any actual code logic:
Code for the Calc.d.ts file:
// File path: Calc.d.ts
// This is a declaration file, containing only type information and no executable code
// Declare the Runoob module, corresponding to the Runoob namespace in the JS library
declare module Runoob
{
// Declare the Calc class, telling TypeScript that this class can be instantiated with new
export
class
Calc
{
// Declare the doSum method: takes a number parameter and returns a number
// Note: only the method signature is declared here, with no method body (curly braces)
doSum
(
limit
:
number
)
:
number
;
}
}
The biggest difference between a declaration file and a regular .ts file: a declaration file has only type signatures, no implementation code. It only answers "what does this function look like", not "what does this function do".
Step 3: Use it in TypeScript code
Now write the business code. After referencing the declaration file, you can use this third-party JS library just like a native TypeScript library:
Code for the CalcTest.ts file:
// File path: CalcTest.ts
// Triple-slash directive: tells the TypeScript compiler to include the declaration file
///
// Create a Calc instance; TypeScript can now correctly identify the type of obj
var
obj
=
new
Runoob.
Calc
(
)
;
// obj.doSum("Hello"); // Compilation error! "Hello" is a string, while doSum requires a number
console.
log
(
obj.
doSum
(
10
)
)
;
// Correct call: pass in 10, expecting 55
Note the line that is commented out:
obj.doSum("Hello");
If you uncomment this line, TypeScript will report an error at compile time, because the declaration file specifies that doSum only accepts a number parameter. This is type checking protecting you — errors are exposed while writing the code, rather than causing a crash only when the browser runs it.
Step 4: Compile the TypeScript
Compile using TypeScript's built-in tsc command:
tsc CalcTest.ts
After compilation, a CalcTest.js file is generated with the following content:
Compiled output of CalcTest.js:
// File path: CalcTest.js (automatically generated by the tsc command)
///
var
obj
=
new
Runoob.
Calc
(
)
;
//obj.doSum("Hello"); // Compilation error
console.
log
(
obj.
doSum
(
10
)
)
;
// Run result: outputs 55 to the console
As you can see, the triple-slash directive and the type information from the declaration file are both gone from the compiled output, leaving only pure JavaScript runtime code.
Step 5: Run in the browser
Finally, create an HTML page to link all the JS files together:
Example
Runoob Tutorial(runoob.com)
Declaration File Test
A quick test by a rookie.
Open this HTML file in a browser, open the console (F12), and you can see the output:
55
This is the return value of doSum(10): 0+1+2+...+10 = 55.

Summary
A declaration file is essentially a "type manual" that enables TypeScript to understand and check pure JavaScript libraries.
The whole workflow can be summarized in three steps:
-
Get a JS library and analyze which APIs it exposes.
-
Write a .d.ts declaration file describing the type signatures of these APIs.
-
Reference the declaration file in your TypeScript code to enjoy full type-checking protection.
For third-party libraries commonly used in daily development, the vast majority already have ready-made declaration files (via npm install @types/xxx npm installation). Only when you use a very obscure library or an internal private library do you need to write a declaration file manually.