所以,另一个问题的一般答案(你好吗? 进口 使用typescript的模块是:
1)创建一个blah.d.ts定义文件。
2)使用:
/// <reference path="./defs/foo/foo.d.ts"/>
import foo = require("foo");
至关重要的是,你需要 都 要加载的node_modules中的文件foo.d.ts和foo.js; 和 NAME foo必须 究竟 两者都匹配。现在...
我想回答的问题是你如何 写 你可以用那种方式导入的打字稿模块?
可以说我有一个这样的模块:
- xq/
- xq/defs/Q.d.ts
- xq/index.ts
- xq/base.ts
- xq/thing.ts
我想从base.ts导出模块'xq'和'Base'类,从thing.ts导出'Thing'。
如果这是javascript中的节点模块,我的index.ts看起来像:
var base = require('./base');
var thing = require('./thing');
module.exports = {
Base: base.Base,
Thing: thing.Thing
};
让我们尝试使用类似的打字稿文件:
import base = require('./base');
export module xq {
export var base = base.Base;
}
调用它:
tsc base.ts index.ts things.ts ... --sourcemap --declaration --target ES3
--module commonjs --outDir dist/xq
怎么了?好吧,我们得到我们的base.d.ts:
export declare class Base<T> {
...
}
和令人兴奋的无用的index.d.ts:
export declare module xq {
var Base: any; // No type hinting! Great. :(
}
并且没有事件导入模块的完全无效的javascript:
(function (xq) {
xq.base = xq.base.Base;
})(exports.xq || (exports.xq = {}));
var xq = exports.xq;
我尝试过一系列关于主题的变化,我唯一可以上班的是:
declare var require;
var base = require('./base');
export module xq {
export var base = base.Base;
}
......但这显然完全破坏了类型检查器。
所以。
打字稿很棒,但这个模块的东西很糟糕。
1)是否可以使用内置定义生成器(我很怀疑)
2)你是如何手工完成的?我在.d.ts文件中看过import语句,我认为这意味着有人已经知道如何做到这一点;那些工作怎么样?如何为具有带import语句的声明的模块执行打字稿?
(例如,我怀疑进行模块声明的正确方法是:
/// <reference path="base.d.ts" />
declare module "xq" {
import base = require('./base');
module xq {
// Some how export the symbol base.Base as Base here
}
export = xq;
}
......但我不知道那些打字稿会是什么样的。