2013-05-05 16 views
5

Sembra che non si può fare questo più a macchina 0,9:tipografico 0.9: funzioni del modulo

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

C'è un modo di creare tipizzazioni che permettono di richiamare il modulo esportato in 0.9?

Si noti che la funzione esportata non ha un nome. Questo è il modo per dichiarare i moduli richiamabili nelle versioni precedenti del dattiloscritto, perché ci sono molti moduli del nodo che esportano solo una funzione.

risposta

6

Il cambiamento appare intenzionale, e non c'è più è un modo per farlo:

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. 

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

+0

E come dovrei aggiornare tali dichiarazioni a 0,9 ?: https: //github.com/soywiz/typescript-node-definitions/blob/master/supertest.d.ts Non sarai in grado di utilizzare quei moduli? – soywiz

+0

+1 poiché risponde. @ Soywiz è una domanda a parte. – basarat

+1

@soywiz - non sparare al messenger :) – JcFx

-1

Ho un esempio di come lo sto facendo.

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

Sembra che è possibile creare un file di my-module.d.ts come questo:

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

Questo vi permetterà di consumare il modulo nel file index.ts :

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

Molto non intuitivo IMO.

Modifica: un'altra trappola che mi ha confuso era la sezione Ambient External Modules, che fa sembrare che il wrapper declare module "my-module" potrebbe essere omesso in questo caso. Qualcuno sa se è possibile?

4

@Weston era vicino, ho trovato è inoltre necessario aggiungere un modulo interno con lo stesso nome della funzione:

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