Reputation: 438
It seems that you can't do this anymore in TypeScript 0.9:
declare module "mymodule" {
export function(): any;
}
Is there a way of creating typings that allow to call the exported module in 0.9?
Notice that the exported function doesn't have a name. That's the way to declare callable modules in previous versions of typescript, because there are a lot of node modules exporting just a function.
Upvotes: 5
Views: 8316
Reputation: 1711
@Weston was close, I found you also need to add an internal module of the same name as the function:
declare module "mymodule" {
function placeholder(): any;
module placeholder {}
export = placeholder;
}
Upvotes: 4
Reputation: 2002
It seems that you can create a my-module.d.ts file like this:
declare module "my-module" {
function placeholder(arg1: any): void;
export = placeholder;
}
This will let you consume the module in your index.ts file:
/// <reference path="./my-module.d.ts" />
import myModule = require("my-module");
myModule("Test Arg");
Very non-intuitive IMO.
Edit: another pitfall that confused me was the Ambient External Modules section, which makes it sound like the declare module "my-module"
wrapper could be omitted in this case. Does anyone know if that's possible?
Upvotes: 3
Reputation: 733
I have an example of how I'm doing it.
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");
Upvotes: -1
Reputation: 11284
The change appears deliberate, and there is no longer a way to do that:
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.
From: http://blogs.msdn.com/b/typescript/archive/2013/04/22/announcing-0-9-early-previews.aspx
Upvotes: 6