DEV Community

Discussion on: Writing a TypeScript Type Definition for a JavaScript npm Package

Collapse
 
joshuatz profile image
Joshua Tzucker • Edited

The reason why adding an import to your *.d.ts file suddenly broke the code that relied on it is because adding imports or exports to a *.d.ts (or really, any *.ts file) changes it from behaving like a script to acting like a module. This is important because scripts have global scope, whereas modules are locally scoped.

If you want to use imports while still having your declarations be "ambient", you just need to wrap them in a declare global {} block.

PS: I feel your frustration with the complexity of ambient declaration docs. I have a cheatsheet, which you might find helpful as it even covers the issue above.

Collapse
 
pahund profile image
Patrick Hund

Cool, thanks for the input!