似乎你不能再在TypeScript 0.9中执行此操作:
declare module "mymodule" {
export function(): any;
}
有没有办法创建允许在0.9中调用导出模块的输入?
请注意,导出的函数没有名称。这是在以前版本的typescript中声明可调用模块的方法,因为有很多节点模块只导出一个函数。
答案 0 :(得分:6)
这种变化似乎是刻意的,而且不再有办法做到这一点:
The ‘module’ keyword no longer creates a type
Description: In 0.9.0, a clearer distinction is made between roles of namespaces, types, and values. Modules now contribute only to namespaces and values and will no longer contribute a type.
Reason: This simplification of the role of modules allow modules to now extend classes and functions more easily.
来自:http://blogs.msdn.com/b/typescript/archive/2013/04/22/announcing-0-9-early-previews.aspx
答案 1 :(得分:4)
@Weston很接近,我发现你还需要添加一个与该函数同名的内部模块:
declare module "mymodule" {
function placeholder(): any;
module placeholder {}
export = placeholder;
}
答案 2 :(得分:3)
您似乎可以创建一个my-module.d.ts文件,如下所示:
declare module "my-module" {
function placeholder(arg1: any): void;
export = placeholder;
}
这将允许您使用index.ts文件中的模块:
/// <reference path="./my-module.d.ts" />
import myModule = require("my-module");
myModule("Test Arg");
非常不直观的IMO。
编辑:困扰我的另一个陷阱是Ambient External Modules部分,这听起来像declare module "my-module"
包装器在这种情况下可以省略。有谁知道这是否可能?
答案 3 :(得分:-1)
我有一个如何做的例子。
https://gist.github.com/danatcofo/9116918
/*
* This is an example of how to turn your typescript modules into functions.
*/
function Example(command: string, ...params: any[]): void;
function Example(command: string, ...params: any[]): any {
var isConstructor = false;
if (this instanceof Example && !this.__previouslyConstructedByExample) {
isConstructor = true;
this.__previouslyConstructedByExample = true;
}
switch (typeof (Example[command])) {
case "function":
if (isConstructor) {
return (function(cls, args){
function F(): void {
return cls.apply(this, args);
}
F.prototype = cls.prototype;
return new F();
})(Example[command], params);
}
return Example[command].apply(Example, params);
case "undefined": throw "unknown command call";
default:
if (isConstructor) throw "unknown command call";
return Example[command];
}
}
module Example {
export function Func0(parm1:string): string {
var ret = "Func0 was called: parm1 = " + parm1;
console.debug(ret);
return ret;
}
export function Func1(parm1:string, parm2: string): string {
var ret = "Func1 was called: parm1 = " + parm1 + ", parm2 = " + parm2;
console.debug(ret);
return ret;
}
export class Test {
public ret: string;
constructor(parm1: string, parm2: string){
this.ret = Func1(parm1, parm2);
}
}
}
var func0 = Example.Func0("hello world");
var func0_fn = <any>Example("Func0", "hello world");
console.assert(func0 == func0_fn, "single param example")
var func1 = Example.Func1("hello", "world");
var func1_fn = <any>Example("Func1", "hello", "world");
console.assert(func1 == func1_fn, "multi param example")
var test = new Example.Test("hello", "world");
var test_fn = new Example("Test", "hello", "world");
console.assert(test instanceof Example.Test, "class example");
console.assert(test_fn instanceof Example.Test, "function class example");