3

Closed

Callable modules not working in TS 0.9

description

This is valid code in TS0.8:
declare module "express" {
    export function (): number;
}

import x = module("express");
x();
Which does not work in TS0.9 probably as a module no longer creates a type.

This is required by a number of nodeJS modules. e.g. express

Usage:
import express = module('express')
var app = <express.ServerApplication> express()
The new design covered at https://typescript.codeplex.com/discussions/444444 caters for creating callable modules however the syntax for declaring a callable modules that can also be loaded via import is still open.
Closed May 24, 2013 at 5:08 PM by paulb
the declare syntax is the following...
declare module 'express' {
    function someName() : number;

    export = someName;
}

comments

Jantrim wrote May 22, 2013 at 12:07 PM

+1, also required for jQuery

declare module "jquery" {
export function (selector: string, context?: any): JQuery;
}

and accoding to require spec, it is possible to export functions
http://requirejs.org/docs/api.html#deffunc

also commonjs talks about factory as a function
http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition

Jantrim wrote May 22, 2013 at 12:26 PM

workaround:
import jq = module("jquery");
$=<jQueryStatic>jq.noConflict();
or
import jq = module("jquery");
jq.noop()
$=<jQueryStatic>jq;
noop() or noConflict() call required for workitem https://typescript.codeplex.com/workitem/1029

paulb wrote May 22, 2013 at 5:43 PM

This is by design see the discussion here:

https://typescript.codeplex.com/discussions/444444

for the new design.

** Closed by paulb 05/22/2013 10:43AM

schungx wrote May 26, 2013 at 4:26 AM

Hi @paulb,

Just tried out the latest 0.9-release (typescript-aeda9c0df4d45816f1ce0026ef81a514dbd6cf8a).

Still gets "error TS1063: Export assignments cannot be used in internal modules." on the line with "export =".

schungx wrote May 27, 2013 at 2:29 PM

Sorry. Problem SOLVED. It errors out if the module name is not put in quotes. It works if the module name is in quotes.

EisenbergEffect wrote Jul 3, 2013 at 1:01 AM

I'm not sure this should be so hastily closed. Regarding jQuery and callable modules (and express and others as well...) I see that you can export the function, but what if there are many overrides of the function which all need to be exported? That is the case with jQuery and I would bet with many Node.js modules as well. What is the syntax for that?

arek_bal wrote Jul 3, 2013 at 10:46 AM

Other problem is that you can not export other symbols that way.
So your exported function can not return "internal" JQuery object.

Scriby wrote Feb 11 at 4:56 PM

For anyone who finds this in the future and just wants to know how to create a callable module, it really helped me to look at the express type definitions here.