最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Export function in Typescript - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论