Currently, I am in the process of creating TypeScript definition files for two libraries that are meant to be used with the new @types
approach. Both libraries adhere to the UMD pattern, allowing them to be consumed either as modules or by referencing them in a <script>
tag.
The first library was relatively easy to define as consumption through both methods would look like this:
import { AccessManager } from 'twilio-common';
const manager = new AccessManager('XXXXXXXX');
or
const manager = new Twilio.AccessManager('XXXXXXXX');
To handle this dual consumption scenario, I utilized the new export as namespace
feature introduced in TypeScript 2:
import { Promise } from 'es6-promise';
export as namespace Twilio;
export class AccessManager {
constructor(initialToken: string);
identity: string | null;
// omitted
}
Now, addressing the second library is where things get a bit more complex and brings me here for assistance.
Consumption via module:
import { AccessManager } from 'twilio-common';
import { Client } from 'twilio-ip-messaging';
const manager = new AccessManager('XXXXXXXX');
const client = new Client(manager);
Consumption via <script>
tag:
const manager = new Twilio.AccessManager('XXXXXXXX');
const client = new Twilio.IPMessaging.Client('XXXXXXXX');
As seen, the contents of the second definition file should be exported using a statement like:
export as namespace Twilio.IPMessaging;
Unfortunately, this solution does not work. In light of this challenge, I am seeking advice on StackOverflow.
Thank you in advance!
Dominik