Junior — Middle
Can you explain what a .d.ts file is in a TypeScript container?
sobes.tech AI
Answer from AI
A .d.ts file in TypeScript is a type declaration file. It contains only type descriptions, interfaces, functions, and other entities, but not actual code. Such files are needed for TypeScript to understand types in JavaScript libraries that are not written in TypeScript, or to separate type definitions from implementation.
For example, if you use a third-party JavaScript library, you can include a .d.ts file with it to get autocompletion and type checking in your TypeScript project.
A simple example of a .d.ts file:
declare module "myLib" {
export function greet(name: string): void;
}
Thus, .d.ts helps integrate JavaScript code with TypeScript, ensuring type safety and development convenience.