jack5891
jack5891

Reputation: 99

Export function in Typescript

I have a .ts file with a module and a function outside module like this:

$(function () {
   populate()
});

function populate() {
...
}

module portfolio.charts {
   export function foo(){
   ...
   }
}

Using Typescript compiler command tsc --declaration the declaration file is created. This .d.ts file contains the following code:

 function populate(): void;
 module portfolio.charts {
       function foo(): void;
 }

Why populate() function and portfolio.charts module are exported? I thought it was necessary the keyword export to export a function or a module. If I add the d.ts file as a dependency on another file I can use all functions and the module. Can I declare them private? Thanks and sorry for my english.

Upvotes: 2

Views: 6328

Answers (1)

Fenton
Fenton

Reputation: 250822

The TypeScript specification is a bit dry on this, so here are some examples.

Example 1

module MyModule {
    class MyClass {
        myFunction() {
            alert('Test');
        }
    }

    function myOtherFunction() {
        alert('Test Again');
    }
}

In this example, MyModule is a global module (it isn't inside of any other module) so this will appear in the definition file. MyClass,myFunction and myOtherFunction are invisible in the definition:

module MyModule {
}

So to make something visible in your declaration, it either...

  1. Needs to be in the global scope, like MyModule or like populate in your example, or

  2. Needs to be prefixed with the export keyword

In your example, point 1 applies.

Upvotes: 7

Related Questions