CodeBlanks Logo Icon CodeBlanks

Declaration file

Write a .d.ts declaration for the package.

.d.ts for a package

When a library package is published, it includes a .d.ts declaration file alongside the JavaScript code. This file lets TypeScript projects using the package get type information. It contains declare statements; no implementation code.

// library.d.ts
export declare function addBook(name: string): string;
export declare const VERSION: string;

Ambient declarations

Each declare statement in a .d.ts file tells TypeScript that the corresponding value exists at runtime. Functions, variables, classes, and modules can be declared as ambient. The compiler checks them but produces no code.

declare function addBook(name: string): string;
declare const VERSION: string;

Practice Write a declaration file

Write an ambient function declaration for kitapEkle(ad: string): string. Write an ambient const declaration for VERSION: string. Then print "bildirim hazır" with console.log.

Starter code

index.ts
// kitapEkle için ambient fonksiyon bildirimi yaz
// VERSION için ambient const bildirimi yaz
// "bildirim hazır" yazdır

Sample solution

index.ts
declare function kitapEkle(ad: string): string;
declare const VERSION: string;
console.log("bildirim hazır");

Expected output

bildirim 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 package's .d.ts file?

  • Runtime implementation code
  • Only comment lines
  • Compilation configuration
  • Type declarations and declare statements Correct answer

Explanation: A .d.ts file contains declare statements and type declarations; no implementation code.

2 Ambient function declaration

Question
____ function kitapEkle(ad: string): string;
Answer
declare function kitapEkle(ad: string): string;

Explanation: The declare keyword is used to tell TypeScript about a function that exists elsewhere.

3 Declaration file concepts

Term Meaning
.d.ts Declaration file extension
declare Ambient value declaration
export declare Package export
ambient Existing in the environment

All lessons in Intermediate