David
David

Reputation: 1529

Importing an interface declared using export = and an ambient module

When a module is implemented in Typescript, I believe it is possible to import various different exports (such as classes, interfaces, variables and enums) that were exported with the old export = syntax.

However when I try this with an ambient module, the compiler (1.8.10) seems to ignore the import.

Declaration File:

//Module declaration
declare module "foo" {
    interface barProc {
        (): any;
    }

    //Note: if I use the function equivalent to the interface this works ok.    
    function worksOk(): any;

    export = barProc;
}

Main File:

//Module usage 
import myFunc = require("foo");
myFunc();

In this case, the compiler complains myFunc is an unknown identifier, and the import line does not appear in the output js file.

Note: in the case illustrated, to keep things simple I did not add any other members to the interface. However the reason for the interface is that the JavaScript library I am modeling has members on the function.

Am I doing something wrong, or is there a workaround for this?

Upvotes: 1

Views: 1057

Answers (1)

Tim Perry
Tim Perry

Reputation: 13216

barProc is an interface, and thus a type. It is not a variable with that type. You can export that, and use it as a type elsewhere, but you can't use it as a callable function, as in your example.

Your example is broadly equivalent to:

interface barProc {
    (): any;
}

barProc();

Put like that, it's pretty clearly wrong. What you want is something more like:

interface barProc {
    (): any;
}

var myFunc: barProc;

myFunc();

Back as a module, that looks like:

declare module "foo" {
    interface barProc {
        (): any;
    }

    var myFunc: barProc;

    export = myFunc;
}


// Elsewhere:

import myFunc = require("foo");
myFunc();

I think that should do what you want.

Upvotes: 1

Related Questions