I'm authoring an npm module in TypeScript https://www.npmjs.com/package/html2commonmark. The module can be used in nodejs (by using require) and from the browser (by loading node_modules/html2commonmark/dist/client/bundle.js in your browser).
I've recently added the *.d.ts files in order to get typing information when using "moduleResolution": "node". This works great, when installing my module it is ready to be used in typescript. Thus: the following piece of typescript code is compiling without errors:
// After installing using npm install html2commonmark
// using "moduleResolution": "node"
import * as html2commonmark from 'html2commonmark';
let converter = new html2commonmark.JSDomConverter();
Beautiful!
Now i want to run my module in a browser. As mentioned before, i need to add a script tag to node_modules/html2commonmark/dist/client/bundle.js to my index.html page. After that, the html2commonmark global variable should be available. The problem is: how do i let the typescript compile know that the global variable is there? The following piece of TS code won't compile:
let converter = new html2commonmark.BrowserConverter();
// error TS2304: Cannot find name 'html2commonmark'.
Even if i add a global.d.ts file, I don't seem to be able to both import my external module and declare my global variable:
// Something like this does not work :(
import * as b from 'html2commonmark';
declare var html2commonmark: typeof b;
I understand why this is. By using the import keyword my ts file is converted to an external module and thus needs to be imported. Yet i feel like my scenario is a common one. Namely: an npm module containing both an npm component and the bundle for the browser which exposes the functionality as a global variable.
Is there any way to declare the global variable using the definition inside my external module? I don't feel like rewriting my api as a (DefinitelyTyped-style) namespace while i just written my entire source code in TS...