We have a TypeScript library that we are publishing to a private NPM environment and we want to use it in other projects whether they are in TS, ES6 or ES5.
Let's say that the library is a npm package called foo
, whose main file works like a "barrel" doing the following:
// Index.ts
import Foo from './Core/Foo';
export {default as Foo} from './Core/Foo';
const foo = new Foo();
export default foo;
We want to export the main class of the library, as well as a default instance of it so that the applications use it without creating a new one, unless it is necessary.
In addition, we have created the definition files in a separate repository similar to DefinitelyTyped:
// foo.d.ts
namespace Foo {
export class Foo {
public constructor()
// ...methods
}
const foo: Foo;
export default foo;
}
module 'foo' {
export = Foo;
}
Running the tests fails with:
TS1063 error: An export assignment can not be used in a namespace.
What we are looking for is to use the default instance as follows:
// ES5, browser env
window.Foo.foo.someMethod();
// ES6/TS
import foo from 'foo';
foo.someMethod();
Any ideas on how to do this correctly?