是否可以从typescript模块导出简单函数?
module SayHi {
export function() {
console.log("Hi");
}
}
new SayHi();
This workitem似乎意味着你不能,但并不是直截了当地说出来.不可能吗?
是否可以从typescript模块导出简单函数?
module SayHi {
export function() {
console.log("Hi");
}
}
new SayHi();
This workitem似乎意味着你不能,但并不是直截了当地说出来.不可能吗?
在那个例子中,很难说你想要什么.exports =
是关于从external个模块导出,但您链接的代码示例是internal个模块.
经验法则:如果你写module foo { ... }
,那你就是在写一个内部模块;如果你在一个文件的顶层写export something something
,你就在写一个外部模块.实际上,在顶层写export module foo
是有点罕见的(从那时起,你会将名称双重嵌套),而在具有顶层导出的文件中写module foo
则更为罕见(因为foo
在外部是不可见的).
以下几点是有意义的(每个场景由一条横向规则描述):
// An internal module named SayHi with an exported function 'foo'
module SayHi {
export function foo() {
console.log("Hi");
}
export class bar { }
}
// N.B. this line could be in another file that has a
// <reference> tag to the file that has 'module SayHi' in it
SayHi.foo();
var b = new SayHi.bar();
file1.ts
// This *file* is an external module because it has a top-level 'export'
export function foo() {
console.log('hi');
}
export class bar { }
file2.ts
// This file is also an external module because it has an 'import' declaration
import f1 = module('file1');
f1.foo();
var b = new f1.bar();
file1.ts
// This will only work in 0.9.0+. This file is an external
// module because it has a top-level 'export'
function f() { }
function g() { }
export = { alpha: f, beta: g };
file2.ts
// This file is also an external module because it has an 'import' declaration
import f1 = require('file1');
f1.alpha(); // invokes f
f1.beta(); // invokes g