2013-05-05 14 views
5

モジュール機能:活字体0.9:あなたが活字体0.9にはもうこれを行うことができないようです

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

は0.9でエクスポートモジュールを呼び出すことができるようにタイピングを作成する方法はありますか?

エクスポートされた関数に名前がないことに注意してください。これは、以前のバージョンのtypescriptで呼び出し可能なモジュールを宣言する方法です。なぜなら、単に関数をエクスポートする多くのノードモジュールが存在するからです。

答えて

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

+0

そして、どのように私は0.9?: httpsにそれらの宣言を更新することになっています: //github.com/soywiz/typescript-node-definitions/blob/master/supertest.d.ts これらのモジュールを使用することはできませんか? – soywiz

+0

+1です。 @soywizは別の質問です。 – basarat

+1

@soywiz - メッセンジャーを撃たないでください:) – JcFx

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

あなたがこのように私の-module.d.tsファイルを作成することができているようです。これは、あなたのindex.tsファイル内のモジュールを消費させます

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

を:

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

非常に非直感的なIMO。

編集:私を混乱させるもう一つの落とし穴は、この場合はdeclare module "my-module"ラッパーのように聞こえるようになるAmbient External Modulesセクションでした。それが可能なら誰でも知っていますか?

4

@Westonが近くにあった、私はあなたにも関数と同じ名前の内部モジュールを追加する必要がありますが見つかりました:

declare module "mymodule" { 
    function placeholder(): any; 
    module placeholder {} 
    export = placeholder; 
} 
関連する問題