Keith
Keith

Reputation: 795

Issue creating a single .d.ts when using external modules

I'm developing an NPM package using typescript. Within this package the TS files are setup as external modules. The compiler won't generate a single .d.ts for external modules. I'm trying to concat all tsc generated type definitions into a single .d.ts for the entire package.

I'm having issues laying out the single .d.ts file (following a similar approach to that used in grunt-dts-bundle). The condensed example below captures my issue.

Given this external module declaration and test file:

test.d.ts :

declare module "ExternalWrapper" {
    export import Foo = require("FooModule");
}

declare module "FooModule" {
    class Foo {
        name: string;
    }
     export = Foo;
}

test.ts:

import externalWrapper = require( 'ExternalWrapper' );
var t = new externalWrapper.Foo();

Running tsc test.ts test.d.ts -m commonjs produces this error: TS2083: Invalid 'new' expression.

If you change 'test.ts' to look like this, importing 'FooModule' directly:

import Foo = require( "FooModule" );
var t = new Foo();

It compiles fine.

The compiler understands the type externalWrapper.Foo however it doesn't seem to represent it as the same type FooModule.Foo. There is something I'm not getting about how the compilers handles modules that are exported via 'export import'.

Failing the above I'll probably look to manually creating the .d.ts :(

Any help appreciated.

Upvotes: 3

Views: 1749

Answers (2)

basarat
basarat

Reputation: 276239

You are probably missing a reference tag:

/// <reference path="test.d.ts"/>

It works :

enter image description here

Upvotes: 2

wjohnsto
wjohnsto

Reputation: 4463

You should be able to fix this by modifying your .d.ts file to resemble the following:

declare module "ExternalWrapper" {
    import FooModule = require("FooModule");
    export var Foo: typeof FooModule;
}

declare module "FooModule" {
    class Foo {
        name: string;
    }
    export = Foo;
}

With the export import syntax the compiler was assuming you were exporting an instance of Foo, not Foo itself... a little quirky.

Upvotes: 2

Related Questions