3

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.

2 Answers 2

2

You are probably missing a reference tag:

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

It works :

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

cheers, that was it. I've seen your issue on external module .d.ts generations over on the typescript issues list, hopefully this goes away if that gets implemented :)
2

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.