2013-05-05 7 views
5

Es scheint, dass Sie dies nicht mehr in Typoskript 0.9 nicht tun können:Typoskript 0,9: Modulfunktionen

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

Gibt es eine Möglichkeit Typisierungen zu schaffen, das die exportierte Modul in 0,9 rufen lassen?

Beachten Sie, dass die exportierte Funktion keinen Namen hat. Auf diese Weise können in früheren Versionen von typescript aufrufbare Module deklariert werden, da viele Knotenmodule nur eine Funktion exportieren.

Antwort

6

Die Änderung erscheint absichtlich, und es gibt nicht mehr eine Möglichkeit, das zu tun:

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. 

Von: http://blogs.msdn.com/b/typescript/archive/2013/04/22/announcing-0-9-early-previews.aspx

+0

Und wie soll ich diese Erklärungen auf 0,9 ?: https aktualisieren: //github.com/soywiz/typescript-node-definitions/blob/master/supertest.d.ts Sie können diese Module nicht verwenden? – soywiz

+0

+1 da es beantwortet. @soywiz das ist eine separate Frage. – basarat

+1

@soywiz - nicht den Messenger schießen :) – JcFx

-1

Ich habe ein Beispiel dafür, wie ich es mache.

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"); 
3

Es scheint, dass Sie ein my-module.d.ts wie diese Datei erstellen:

declare module "my-module" { 
    function placeholder(arg1: any): void; 
    export = placeholder; 
} 

So können Sie das Modul in Ihrer index.ts Datei verbrauchen :

/// <reference path="./my-module.d.ts" /> 
import myModule = require("my-module"); 
myModule("Test Arg"); 

Sehr nicht intuitive IMO.

Edit: ein weiterer Fehler, der mich verwirrt war der Ambient External Modules Abschnitt, der es klingt wie die declare module "my-module" Wrapper in diesem Fall weggelassen werden könnte. Weiß jemand, ob das möglich ist?

4

@Weston war in der Nähe, fand ich auch ein internes Modul mit dem gleichen Namen wie die Funktion hinzufügen müssen:

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