What is .d.ts?
- TypeScript course
- Intermediate
- Lesson 58/89
- 4 min
Learn what declaration files are.
What is a .d.ts?
A .d.ts file is a TypeScript declaration file. It contains only type information; no executable JavaScript code. It is used to add types to JavaScript libraries on the TypeScript side. The declare keyword tells TypeScript about a value that already exists outside.
// global.d.ts
declare const VERSION: string;Difference between .d.ts and .ts
A .ts file holds both types and implementation; when compiled it produces .js. A .d.ts file holds only type declarations and is not turned into .js by the compiler. That is why you use .d.ts for JS libraries that need types.
// app.d.ts (declaration)
export const PI: number;
// app.ts (implementation)
export const PI = 3.14;Practice Declare topla ambient function
Declare an ambient function topla that takes two number parameters and returns number. Then print "topla hazır" with console.log.
Starter code
// topla fonksiyonunu declare ile bildir
// (a: number, b: number) -> number
console.log("topla hazır");Sample solution
declare function topla(a: number, b: number): number;
console.log("topla hazır");Expected output
topla hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which of the following is found in a .d.ts file?
- Only comment lines
- Type declarations Correct answer
- Type declarations and implementation
- Only JavaScript code
Explanation: A .d.ts file contains only type declarations; it does not host any implementation or executable code.
2 Declare the VERSION global
// VERSION globalini ambient olarak bildir
____ const VERSION: string;
// VERSION globalini ambient olarak bildir
declare const VERSION: string;
Explanation: The `declare` keyword is used to declare an ambient value; it tells TypeScript that the value already exists elsewhere.
3 .d.ts concepts
| Term | Meaning |
|---|---|
.d.ts |
Declaration file extension |
declare |
Declares an existing value |
tür bilgisi |
Content without implementation |
.ts |
Both type and implementation |