5

在 TypeScript 0.9 中你似乎不能再这样做了:

declare module "mymodule" {
    export function(): any;
}

有没有一种方法可以创建允许在 0.9 中调用导出模块的类型?

请注意,导出的函数没有名称。这是在以前版本的 typescript 中声明可调用模块的方式,因为有很多节点模块只导出一个函数。

4

4 回答 4

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

于 2013-05-06T09:50:20.933 回答
4

@Weston 很接近,我发现您还需要添加一个与函数同名的内部模块:

declare module "mymodule" {
    function placeholder(): any;
    module placeholder {}
    export = placeholder;
}
于 2016-01-11T23:45:09.410 回答
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"在这种情况下可以省略包装器。有谁知道这是否可能?

于 2015-10-20T02:25:55.077 回答
-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");
于 2014-02-21T16:09:14.630 回答