Q42. What are declaration files (.d.ts)?
Declaration files describe the types of JavaScript modules without providing implementation. They let TypeScript consumers type-check calls to JavaScript libraries. Declarations may be bundled with a package or supplied by community type packages, and must accurately match runtime behavior.
A .d.ts file can describe exported functions and classes, but it does not implement them.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
What does a declaration file primarily provide?