如何将javascript AMD模块导入外部TypeScript模块? [英] How can I import a javascript AMD module into an external TypeScript module?
问题描述
如何将javascript AMD模块导入外部TypeScript模块?
How can I import a javascript AMD module into a external TypeScript module?
我正在模块化我的客户端TypeScript程序,以使用Bower管理的AMD模块. 作为此过程的一部分,Typescript模块成为javascript AMD模块,然后我将其发布. 现在,我有一个要包含在TypeScript模块中的javascript AMD模块,并且我不想使用javascript AMD发布原始的TypeScript.
I'm modularizing my client TypeScript program to use AMD modules managed by bower. As part of this process, a Typescript module becomes 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.
我无法弄清楚如何编写TypeScript代码,以便它将加载没有相应TypeScript的JavaScript AMD模块,而且看来最新版本的TypeScript尚不支持此功能.
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.
如果我从0.9.7 TypeScript规范的示例开始,请参见11.2节:
If I start with the example from the 0.9.7 TypeScript specification, section 11.2:
文件main.ts:
import log = require("./log");
log.message("hello");
文件log.ts:
export function message(s: string) {
console.log(s);
}
我相信我应该能够修改main.ts,以便编译器解析对log.js AMD模块的"log"引用. 以下是运行 tsc --module amd main.ts 生成的log.js文件.这是正确的AMD模块.
I believe I should be able to modify main.ts so that the compiler 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.
文件log.js:
define(["require", "exports"], function(require, exports) {
function message(s) {
console.log(s);
}
exports.message = message;
});
要模拟具有javascript AMD模块的代码,请编译以上示例,然后删除log.ts文件. 如果现在尝试使用相同的命令进行编译,则它将失败,并显示以下错误:
To simulate having a javascript AMD module, compile the above example, and then delete the log.ts file. If you now try to compile using the same command, 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.
我现在如何修改main.ts,使其针对此log.js AMD模块进行编译和解析? 我可以根据需要编写log.d.ts文件,但是希望没有声明文件也可以使用的方法.
How can I now modify main.ts so it compiles 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.
如果我能以规范的TypeScript方式学习如何做到这一点,那么我就可以继续项目的完整模块化.
If I can learn how to do this in the canonical TypeScript Way, then I can continue the complete modularization of my project.
推荐答案
现在如何修改main.ts,以便可以加载和使用此log.js AMD模块?
How can I now modify main.ts so I can load and use this log.js AMD module?
如果只有log.js可用,则可以使用declare
告诉打字稿有关log
中类型信息的信息,即创建log.d.ts
:
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);
}
然后从main.ts中将其用作:
And then use it from main.ts as :
/// <reference path='log.d.ts'/>
import log = require('log');
log.message("hello");
这篇关于如何将javascript AMD模块导入外部TypeScript模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!