2013-05-05 9 views
5

ऐसा लगता है कि आप यह अब और टाइपप्रति 0.9 में ऐसा नहीं कर सकते:टाइपप्रति 0.9: मॉड्यूल कार्यों

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

वहाँ typings कि 0.9 में निर्यात मॉड्यूल कॉल करने की अनुमति बनाने का एक तरीका है?

ध्यान दें कि निर्यात किए गए फ़ंक्शन का नाम नहीं है। टाइपस्क्रिप्ट के पिछले संस्करणों में कॉल करने योग्य मॉड्यूल घोषित करने का यही तरीका है, क्योंकि केवल एक फ़ंक्शन निर्यात करने वाले बहुत सारे नोड मॉड्यूल हैं।

उत्तर

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 इस तरह फाइल बना सकते हैं कि:

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

बहुत गैर-सहज ज्ञान युक्त आईएमओ।

संपादित करें: मुझे परेशान करने वाला एक और गड़बड़ Ambient External Modules अनुभाग था, जो इस मामले में declare module "my-module" रैपर को छोड़ा जा सकता है। क्या किसी को पता है कि यह संभव है?

4

@Weston करीब था, मैंने पाया आप भी समारोह के रूप में एक ही नाम के एक आंतरिक मॉड्यूल जोड़ने की जरूरत:

declare module "mymodule" { 
    function placeholder(): any; 
    module placeholder {} 
    export = placeholder; 
} 
संबंधित मुद्दे