Declaration files tell TypeScript about the shape of code written in JavaScript or other files. They help TypeScript understand what types and functions exist without running the code.
Declaration file syntax (.d.ts) in Typescript
declare module 'module-name' { export function functionName(param: type): returnType; export interface InterfaceName { property: type; } // other declarations } declare const variableName: type; declare function functionName(param: type): returnType; interface InterfaceName { property: type; } // No implementation code, only declarations
declare keyword to tell TypeScript about existing code without implementing it.add and subtract, both taking two numbers and returning a number.declare module 'math-lib' { export function add(a: number, b: number): number; export function subtract(a: number, b: number): number; }
VERSION of type string available globally.declare const VERSION: string;User describing an object with three properties.interface User {
id: number;
name: string;
isActive: boolean;
}greet that takes a string and returns nothing (void).declare function greet(name: string): void;
This example shows a declaration file math-lib.d.ts describing a module with a function and a constant. The usage file imports them and uses them to calculate and print the area of a circle.
/* math-lib.d.ts */ declare module 'math-lib' { export function multiply(x: number, y: number): number; export const PI: number; } /* usage.ts */ import { multiply, PI } from 'math-lib'; const area = multiply(PI, 5 * 5); console.log(`Area of circle: ${area}`);
Declaration files do not contain real code, so running the sample program without the actual module will print NaN because multiply and PI are not implemented.
Declaration files help TypeScript check your code but do not create JavaScript output themselves.
Declaration files (.d.ts) describe the shape of existing code for TypeScript.
They use declare to define types, functions, variables, and modules without implementation.
They help TypeScript understand JavaScript libraries and improve code safety.
Practice
.d.ts)?Solution
Step 1: Understand the role of declaration files
Declaration files provide type information about existing JavaScript code but do not contain actual code implementations.Step 2: Differentiate from other file types
Unlike.tsfiles that contain executable code,.d.tsfiles only describe types and interfaces.Final Answer:
To describe the types and structure of existing JavaScript code without implementation -> Option AQuick Check:
Declaration files describe types only [OK]
- Thinking declaration files contain executable code
- Confusing declaration files with config files
- Assuming declaration files compile to JavaScript
greet that takes a string and returns void in a .d.ts file?Solution
Step 1: Recognize declaration syntax in .d.ts files
Functions must be declared with thedeclarekeyword and no implementation body.Step 2: Check each option
function greet(name: string): void; missesdeclare, declare greet(name: string): void; has incorrect syntax, function greet(name: string): void {} includes a function body which is not allowed in declaration files.Final Answer:
declare function greet(name: string): void; -> Option DQuick Check:
Usedeclare functionwithout body [OK]
- Omitting the 'declare' keyword
- Adding function bodies in declaration files
- Using incorrect declaration syntax
.d.ts file:declare module "math-lib" {
export function add(a: number, b: number): number;
}What will be the TypeScript type of
result after this code?import { add } from "math-lib";
const result = add(5, 3);Solution
Step 1: Analyze the declaration file
The module "math-lib" declares an exported functionaddthat takes two numbers and returns a number.Step 2: Understand the import and usage
The code importsaddand calls it with two numbers, so the result type matches the declared return type: number.Final Answer:
number -> Option CQuick Check:
Declared return type = number [OK]
- Assuming return type is any by default
- Confusing return type with parameter types
- Ignoring module declaration types
.d.ts declaration:declare var count: number = 10;
Solution
Step 1: Understand variable declaration in .d.ts files
Declaration files only declare types and variables without assigning values.Step 2: Check the given code
The code assigns= 10which is not allowed in declaration files.Final Answer:
Variables cannot be declared with initial values in .d.ts files -> Option AQuick Check:
No initial values allowed in declarations [OK]
- Adding initial values to declared variables
- Removing 'declare' keyword mistakenly
- Using invalid keywords in declaration files
.d.ts file?Solution
Step 1: Understand the object shape needed
The library exports an object where keys are strings and values are functions returning numbers.Step 2: Match syntax for index signatures in declaration files
declare const lib: { [key: string]: () => number }; export default lib; uses the correct index signature syntax{ [key: string]: () => number }withdeclareand exports default.Step 3: Eliminate incorrect options
const lib: { string: () => number }; export default lib; lacksdeclareand uses invalid syntax, declare const lib: { key: string; value: () => number }; export default lib; incorrectly declares keys and values as properties, declare const lib: Map number>; export default lib; uses Map which is not specified.Final Answer:
declare const lib: { [key: string]: () => number }; export default lib; -> Option BQuick Check:
Use index signature with declare and export default [OK]
- Using invalid object property syntax
- Omitting 'declare' keyword
- Confusing Map with plain object types
Ready to prove it?
Pass the test and this pattern moves unknown
