We are currently developing a TypeScript library that will be published to our private NPM environment. The goal is for this library to be usable in TS, ES6, or ES5 projects.
Let's call the npm package foo
. The main file of the library serves as an entry point and performs the following:
// Index.ts
import Foo from './Core/Foo';
export {default as Foo} from './Core/Foo';
const foo = new Foo();
export default foo;
Our aim is to export both the main library class and have a default instance available for easy use by applications without the need to create a new one when not required.
In addition, we have created type definition files in a separate repository similar to DefinitelyTyped:
// foo.d.ts
declare namespace Foo {
export class Foo {
public constructor()
// ...methods
}
const foo: Foo;
export default foo;
}
declare module 'foo' {
export = Foo;
}
Unfortunately, running tests on the library results in an error message:
error TS1063: An export assignment cannot be used in a namespace.
The desired usage for the default instance should look like this:
// ES5, browser env
window.Foo.foo.someMethod();
// ES6/TS
import foo from 'foo';
foo.someMethod();
Are there any suggestions for correcting this issue?
UPDATE
Prior to @daniel-rosenwasser's suggestion, declaring just the module worked fine. However, a problem arose when attempting to create a new module that extended the initial one.
For example:
// bar.d.ts
/// <reference path="../foo/foo.d.ts"/>
import {
Foo
} from 'foo';
declare module 'bar' {
export class Bar extends Foo {
public constructor();
// more methods
}
}
And its corresponding tests:
// bar-tests.ts
/// <reference path="../foo/foo.d.ts"/>
/// <reference path="./bar.d.ts"/>
import foo, {
Foo
} from 'foo';
import {
Bar
} from 'bar';
namespace TestBar {
{
let result: Foo;
result = foo;
}
{
let result: Foo;
result = new Bar();
}
}
This time, the errors received are:
bar/bar-tests.ts: error TS2307: Cannot find module 'bar'.
bar/bar.d.ts: error TS2664: Invalid module name in augmentation, module 'bar' cannot be found.