Problem of creating one .d.ts when using external modules

I am developing an NPM package using typescript. In this package, TS files are installed as plug-ins. The compiler will not generate a single .d.ts for external modules. I'm trying to combine all the tsc type definitions into one .d.ts for the whole package.

I am having trouble generating a single .d.ts file (following a similar approach to the one used in grunt-dts-bundle

). Below is a condensed example.

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

throws this error:TS2083: Invalid 'new' expression.

If you change "test.ts" to import "FooModule" directly:

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

      

It compiles fine.

The compiler understands a type externalWrapper.Foo

, but it does not represent it as the same type FooModule.Foo

. I don't understand a thing or two about how compilers handle modules that are exported via "export import".

Failing above, I'll probably take a look at how to manually create the .d.ts :(

Any help was appreciated.

+3


source to share


2 answers


You are probably missing the tag reference

:

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

      



Works:

enter image description here

+2


source


You can fix this by modifying your .d.ts file to look like this:

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

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

      



With the syntax, the export import

compiler assumed that you were exporting an instance of Foo and not Foo itself ... a little freaky.

+2


source







All Articles