21

I would like to create a project using Typescript modules, however allow it to be consumed from vanilla javascript.

Lets say it contains 3 modules each containing a single class, A, B, and C.

i.e.

A.ts:

export default class A {
    /* things */
}

B.ts:

export default class B {
    /* things */
}

C.ts:

export default class C {
    /* things */
}

All of these are built and bundled into one dist.js file with webpack. I would like the user of the library to be able to do something akin to

<script src="dist.js"></script>
<script>
    var foo = new LettersLibrary.A();
</script>

how would I go about doing this, ultimately the goal is to be able to develop taking advantage of typescript modules, but provide a library usable from vanilla js.

1

1 Answer 1

15

Use a TypeScript Namespace for this. You can declare your classes inside it and then export them from inside the module. Your user will then be able to use it like you want.

https://www.typescriptlang.org/docs/handbook/namespaces.html

Example:

namespace LettersLibrary {
  export class A {
    hello = "Hello World";
  }

  export class B {
    myBool = false;
  }

  export class C {
    someInt = 42;
  }
}

In JavaScript, you would then do:

const instance = new LettersLibrary.A ();
console.log (instance.hello); // "Hello World"

If you need to re-export classes from other files, just export the imported class as const and type (useful for TypeScript development, otherwise you will not be able to use the type from the module):

import importA from "...";
import importB from "...";
import importC from "...";

namespace LettersLibrary {
    export const A = importA;
    export type A = importA;

    // Same for B and C
}

When using WebPack, you will have to export it as a library. For this, you can use the libraryExport configuration together with the library and libraryTarget options. See: https://webpack.js.org/configuration/output/#output-libraryexport

Thanks to @Ghabriel Nunes, who informed me that modules are now named namespaces.

20
  • 1
    Sure. Just import the class you need and then use export const A = A; and export type A = A; combined to have a fully functional re-exported class. I've updated my answer.
    – NikxDa
    Dec 14, 2017 at 13:58
  • 1
    @GhabrielNunes From what I know, this is not quite true. Internal modules are now namespaces, while external modules are just modules. At least this is what the official TypeScript site says.
    – NikxDa
    Dec 14, 2017 at 14:06
  • 4
    external modules are just modules means that you just export whatever classes/variables you want directly instead of wrapping them in a namespace. The module keyword itself is completely equivalent to namespace. See here Dec 14, 2017 at 14:13
  • 1
    This is not a TypeScript, but rather a WebPack issue. You can configure WebPack to make your code available as a library, as described here: stackoverflow.com/questions/34357489/…
    – NikxDa
    Dec 14, 2017 at 14:27
  • 1
    Adding libraryTarget: 'var' and library: 'LettersLibrary to the output allows me to access a LettersLibrary' from javascript, but A, B, and C` remain undefined. edit: It works, I just have to do LettersLibrary.LettersLibrary.A, I'm going to remove the namespace created per this answer and hopefully it works Dec 14, 2017 at 14:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.