admin管理员组

文章数量:1332377

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 piler mand 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.

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 piler mand 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.

Share Improve this question asked Mar 12, 2013 at 15:25 jack5891jack5891 992 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.

本文标签: javascriptExport function in TypescriptStack Overflow