0

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?

1 Answer 1

1

Try this:

// log2.d.ts
declare module 'log2'{
    function message(s: string);
    export = message;
}

See also: the Handbook.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.