Summary - Declaration files
- TypeScript course
- Intermediate
- Lesson 61/89
- 4 min
Add types to a JS library by writing a .d.ts file.
Summary - Declaration files
In this group you learned .d.ts files. They add types to JS libraries. You use declare for existing values, declare module for ambient modules, and /// <reference> to reference other files.
declare module "x" {
export function fn(): void;
}
/// <reference path="./x.d.ts" />Practice Declare the "araclar" ambient module
Declare an ambient module "araclar". Export a function topla(a: number, b: number): number inside it. Then print "araclar hazır" with console.log.
Starter code
// "araclar" ambient modülünü bildir
// içinde topla(a: number, b: number): number dışa aktar
console.log("araclar hazır");Sample solution
declare module "araclar" {
export function topla(a: number, b: number): number;
}
console.log("araclar hazır");Expected output
araclar hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is used to add types to a JS library on the TypeScript side?
- A .d.ts declaration file Correct answer
- A package.json package file
- A tsconfig.json configuration file
- A JavaScript file (.js)
Explanation: .d.ts files are used to add type information to JS libraries; tsconfig and package.json do not define types.
2 topla return type
declare function topla(a: number, b: number): ____;
declare function topla(a: number, b: number): number;
Explanation: The return type of an ambient function that adds two numbers should be `number`.
3 Declaration files summary
| Term | Meaning |
|---|---|
.d.ts dosyası |
Contains types, not code |
declare anahtar kelimesi |
Used for declarations |
declare module |
Gives types to a JS module |
reference yönergesi |
References another .d.ts file |