Reputation: 836
Related question: How can I import a javascript AMD module into an external TypeScript module?
I have tried the workaround in the question above, it surely works. But it did not work when the AMD module returns function itself as the module (not {message:Function}
, but Function
itself) using module.exports=function ..
log.js: (same as the question above)
define(["require", "exports"], function(require, exports) {
function message(s) {
console.log(s);
}
exports.message = message;
});
log.d.ts:(same)
declare module 'log'{
export function message(s:string);
}
log2.js:
define(["require", "exports","module"],function(require, exports,module) {
function message(s) {
console.log(s);
}
module.exports = message;
});
log2.d.ts:
declare module 'log2'{
export default function (s:string);
}
main.ts:
import log = require('log');
log.message("hello"); // It works
import log2 = require('log2');
log2("hello"); // Error error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof import("log2")' has no compatible call signatures.
log2.default("hello"); // Compile passed. But runtime error on the browser (Uncaught TypeError: log2.default is not a function)
I compiled these *.ts files with tsc -m amd
(tsc Version 3.2.4), and used require.js 2.3.5 to run the program on the browser. But log2 does not run( See main.ts ).
Maybe I am misunderstanding the use of export default
in log2.d.ts . How can I write the type definition correctly?
Upvotes: 0
Views: 151
Reputation: 23692
Try this:
// log2.d.ts
declare module 'log2'{
function message(s: string);
export = message;
}
See also: the Handbook.
Upvotes: 1