Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to augment an UMD module definition in TypeScript 2

I'm currently writing TypeScript definition files for two libraries that should be consumed through the new @types approach. Both follow the UMD pattern. You can either consume them as a module or by referencing them in a <script> tag.

The first one was straight forward to write since the two ways of consumption would look like this:

import { AccessManager } from 'twilio-common';
const manager = new AccessManager('XXXXXXXX');

or

const manager = new Twilio.AccessManager('XXXXXXXX');

I solved this by using the new export as namespace feature of TypeScript 2:

import { Promise } from 'es6-promise';

export as namespace Twilio;

export class AccessManager {
  constructor(initialToken: string);
  identity: string | null;
  // omitted
}

Now for the second module this becomes a bit more tricky and why I'm writing here.

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');

So as you can see the things that are exported in the second defintion files should be exported using a line like:

export as namespace Twilio.IPMessaging;

However that doesn't work. Since I can't find out what's the proper solution here, I'm turning to StackOverflow for advice.

Thanks a lot in advance!

Dominik

like image 600
dkundel Avatar asked Dec 05 '25 16:12

dkundel


1 Answers

You could also declare a twilio namespace with a sub namespace of IPMessaging and export "Twilio" as the global namespace.

It will make Twilio show up as an importable target for modules though

like image 78
basarat Avatar answered Dec 08 '25 08:12

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!