最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I import a javascript AMD module into an external TypeScript module? - Stack Overflow

programmeradmin4浏览0评论

How can I import a javascript AMD module into a external TypeScript module?

I'm modularizing my client TypeScript program to use AMD modules managed by bower. As part of this process, a Typescript module bees a javascript AMD module, which I then publish. Now I have a javascript AMD module that I want to include in a TypeScript module, and I don't want to publish the original TypeScript with the javascript AMD.

I cannot figure out how to write my TypeScript code so that it will load a javascript AMD module for which there is no corresponding TypeScript, and it appears that the most recent versions of TypeScript don't support this yet.

If I start with the example from the 0.9.7 TypeScript specification, section 11.2:

File main.ts:

import log = require("./log"); 
log.message("hello"); 

File log.ts:

export function message(s: string) { 
    console.log(s); 
}

I believe I should be able to modify main.ts so that the piler resolves the "log" reference to the log.js AMD module. Below is the log.js file that was produced by running tsc --module amd main.ts. It is a correct AMD module.

File log.js:

define(["require", "exports"], function(require, exports) {
    function message(s) {
        console.log(s);
    }
    exports.message = message;
});

To simulate having a javascript AMD module, pile the above example, and then delete the log.ts file. If you now try to pile using the same mand, it fails with the following errors:

./main.ts(1,1): error TS2071: Unable to resolve external module '"./log"'.  
./main.ts(1,1): error TS2072: Module cannot be aliased to a non-module type.

How can I now modify main.ts so it piles and resolves against this log.js AMD module? I can write log.d.ts file if I have to, but would like a method that also works without a declaration file.

If I can learn how to do this in the canonical TypeScript Way, then I can continue the plete modularization of my project.

How can I import a javascript AMD module into a external TypeScript module?

I'm modularizing my client TypeScript program to use AMD modules managed by bower. As part of this process, a Typescript module bees a javascript AMD module, which I then publish. Now I have a javascript AMD module that I want to include in a TypeScript module, and I don't want to publish the original TypeScript with the javascript AMD.

I cannot figure out how to write my TypeScript code so that it will load a javascript AMD module for which there is no corresponding TypeScript, and it appears that the most recent versions of TypeScript don't support this yet.

If I start with the example from the 0.9.7 TypeScript specification, section 11.2:

File main.ts:

import log = require("./log"); 
log.message("hello"); 

File log.ts:

export function message(s: string) { 
    console.log(s); 
}

I believe I should be able to modify main.ts so that the piler resolves the "log" reference to the log.js AMD module. Below is the log.js file that was produced by running tsc --module amd main.ts. It is a correct AMD module.

File log.js:

define(["require", "exports"], function(require, exports) {
    function message(s) {
        console.log(s);
    }
    exports.message = message;
});

To simulate having a javascript AMD module, pile the above example, and then delete the log.ts file. If you now try to pile using the same mand, it fails with the following errors:

./main.ts(1,1): error TS2071: Unable to resolve external module '"./log"'.  
./main.ts(1,1): error TS2072: Module cannot be aliased to a non-module type.

How can I now modify main.ts so it piles and resolves against this log.js AMD module? I can write log.d.ts file if I have to, but would like a method that also works without a declaration file.

If I can learn how to do this in the canonical TypeScript Way, then I can continue the plete modularization of my project.

Share Improve this question edited Mar 30, 2014 at 2:22 psnider asked Mar 29, 2014 at 21:49 psniderpsnider 2573 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

How can I now modify main.ts so I can load and use this log.js AMD module?

If you only the log.js available you can tell typescript about the type information from log using declare i.e. create a log.d.ts:

declare module 'log'{
    export function message(s:string);
}

And then use it from main.ts as :

/// <reference path='log.d.ts'/>    

import log = require('log'); 
log.message("hello"); 
发布评论

评论列表(0)

  1. 暂无评论