Why do we need declaration files (.d.ts) in TypeScript projects?
Think about how TypeScript understands code from plain JavaScript libraries.
Declaration files tell TypeScript about the types in JavaScript libraries, so it can check your code safely without running it.
What happens when you try to use a JavaScript library in TypeScript without a declaration file?
import { foo } from 'some-js-lib';
foo();TypeScript needs type info to understand external code.
Without declaration files, TypeScript cannot find type info for the library and shows an error during compilation.
Given this error in a TypeScript project:
error TS7016: Could not find a declaration file for module 'my-lib'.
Which option correctly fixes this error?
Look for official type packages on npm.
Many JavaScript libraries have separate type declaration packages under @types. Installing them provides the needed type info.
Which of the following is a correct way to declare a function in a TypeScript declaration file (.d.ts)?
Declaration files only describe types, no function bodies.
In declaration files, functions are declared with declare and no implementation is provided.
You want to use a JavaScript library math-tools in your TypeScript project, but it has no built-in types. You create a math-tools.d.ts file with the following content:
declare module 'math-tools' {
export function add(a: number, b: number): number;
export function multiply(a: number, b: number): number;
}What is the effect of adding this declaration file?
Declaration files provide type info but do not change runtime code.
The declaration file tells TypeScript about the types of the library's functions, enabling type checking without affecting runtime behavior.