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

javascript - Typescript - module functions reference in different file - "could not find symbol" - Stack Overf

programmeradmin1浏览0评论

Apologies if the answer to this is simple, or I've mis-read the typescript documentation, but..

I have a module:

module Utils{

    export function MyFunction(str: string):string {
        return // something to do with string
    }

}

I want to use this in another .ts file (Clients.ts) so at the top I add a reference and try to use it:

/// <reference path="Utils.ts" />
Utils.MyFunction(str);

But get the following error:

/*

Compile Error. 
See error list for details
 [path]/Clients.ts(26,39): error TS2095: Could not find symbol 'Utils'.
 [path]/Clients.ts(30,49): error TS2095: Could not find symbol 'Utils'.
error TS5037: Cannot pile external modules unless the '--module' flag is provided.


*/

Could anybody explain what I'm doing wrong?

Using VS2012, with Web Essentials and TypeScript 0.9.1

Thanks.

Apologies if the answer to this is simple, or I've mis-read the typescript documentation, but..

I have a module:

module Utils{

    export function MyFunction(str: string):string {
        return // something to do with string
    }

}

I want to use this in another .ts file (Clients.ts) so at the top I add a reference and try to use it:

/// <reference path="Utils.ts" />
Utils.MyFunction(str);

But get the following error:

/*

Compile Error. 
See error list for details
 [path]/Clients.ts(26,39): error TS2095: Could not find symbol 'Utils'.
 [path]/Clients.ts(30,49): error TS2095: Could not find symbol 'Utils'.
error TS5037: Cannot pile external modules unless the '--module' flag is provided.


*/

Could anybody explain what I'm doing wrong?

Using VS2012, with Web Essentials and TypeScript 0.9.1

Thanks.

Share Improve this question asked Aug 29, 2013 at 9:27 LiverpoolsNumber9LiverpoolsNumber9 2,3943 gold badges23 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Found the answer myself. What I was actually looking for was a static method on a class. As per the below:

class Utils {

    static MyFunction(str: string): string {
        return //... do something with string
    }
}

And this worked in the Client.ts

/// <reference path="Utils.ts" />
var x = Utils.MyFunction(str);

The error you are getting error TS5037: Cannot pile external modules unless the '--module' flag is provided. is not something you would get with your code.

You would get this only if you export something at the root level of your file. e.g.

export module Utils{ // Notice the export keyword 

    export function MyFunction(str: string):string {
        return // something to do with string
    }

}

In this case you are using an external module loader and typescript needs to know if its amd(requirejs/browser) or monjs(node/server side)

PS: I did a video on the subject : http://www.youtube./watch?v=KDrWLMUY0R0&hd=1

发布评论

评论列表(0)

  1. 暂无评论