关于混合 commonJS 和 ES6 模块系统的几个问题
few questions on mixing the commonJS and ES6 module system
我的问题是关于使用 commonJS(节点样式)和 ES6 模块(使用打字稿)。我有这个 angular 1.5 应用程序,它使用 commonJS 模块系统。我试图使用打字稿在应用程序中只创建一个工厂。几个相同的问题。
我们可以使用 typescript import 关键字来导入导出的模块吗
使用 commonJS module.exports 语法?例如,假设我们有
RandomService.js 下面我们希望在我们的打字稿文件中使用。我
注意到做一个 import * as randomservice from
'../../services/RandomService.js' 引发了一些与 not 相关的错误
能够找到模块。我通过使用
require(),但只是想知道这是否可以在
打字稿?
var randomString = require('random-string');
module.exports = {
getSuperRandom: function() {
var x = 'super-random-' + randomString();
return x;
}
}
当我们从 typescript 文件导出模块时,通常会导出
object 有一个 属性 来保存我们的对象。甚至做出口
default 导致 .default 属性 具有我们导出的对象。
无论如何我们可以将导出的对象直接设置为
导出对象(就像我们可以在 commonJS 模块系统中做的那样
我们做 module.exports = 'helloservice')?
- 是的,可以导入用 javascript 编写的 commonJS 模块,但前提是打字稿编译器可以为这些模块找到 declarations。
例如,如果您在 module1.js
中有此 javascript 模块:
exports.f = function(s) {
return s.length
}
您必须在文件 module1.d.ts
:
中提供描述模块并定义其导出类型的声明文件
export declare function f(s: string): number;
然后您可以将此模块与 import
一起使用。如果您将此代码放在与 module1.d.ts
:
相同目录中的 test.ts
文件中,Typescript 将找到导入的模块
import * as module1 from './module1';
let n: number = module1.f('z');
如果您使用 --module=commonjs
(默认设置)编译此代码,您将得到非常正常的 commonjs 代码:
"use strict";
var module1 = require('./module1');
var n = module1.f('z');
如果你的模块导出一些对象,例如像这样 module2.js
:
module.exports = {
a: 'a',
n: 1
};
最好避免使用 es6 语法导入它 - es6 始终假定模块导出命名空间,而不是对象。在打字稿中,有一种叫做 import require 的特殊语法:
import m2 = require('./module2');
let n1: string = m2.a;
声明文件,module2.d.ts
,必须使用export assignment
语法:
export = {} as {a: string, n: number};
实际值 {}
在声明文件中无关紧要,可以是任何东西 - 只有类型很重要(作为未命名接口的类型转换给出)。
如果一个模块导出一个字符串,就像你的例子一样,声明可以像
一样简单
export = '';
我的问题是关于使用 commonJS(节点样式)和 ES6 模块(使用打字稿)。我有这个 angular 1.5 应用程序,它使用 commonJS 模块系统。我试图使用打字稿在应用程序中只创建一个工厂。几个相同的问题。
我们可以使用 typescript import 关键字来导入导出的模块吗 使用 commonJS module.exports 语法?例如,假设我们有 RandomService.js 下面我们希望在我们的打字稿文件中使用。我 注意到做一个 import * as randomservice from '../../services/RandomService.js' 引发了一些与 not 相关的错误 能够找到模块。我通过使用 require(),但只是想知道这是否可以在 打字稿?
var randomString = require('random-string'); module.exports = { getSuperRandom: function() { var x = 'super-random-' + randomString(); return x; } }
当我们从 typescript 文件导出模块时,通常会导出 object 有一个 属性 来保存我们的对象。甚至做出口 default 导致 .default 属性 具有我们导出的对象。 无论如何我们可以将导出的对象直接设置为 导出对象(就像我们可以在 commonJS 模块系统中做的那样 我们做 module.exports = 'helloservice')?
- 是的,可以导入用 javascript 编写的 commonJS 模块,但前提是打字稿编译器可以为这些模块找到 declarations。
例如,如果您在 module1.js
中有此 javascript 模块:
exports.f = function(s) {
return s.length
}
您必须在文件 module1.d.ts
:
export declare function f(s: string): number;
然后您可以将此模块与 import
一起使用。如果您将此代码放在与 module1.d.ts
:
test.ts
文件中,Typescript 将找到导入的模块
import * as module1 from './module1';
let n: number = module1.f('z');
如果您使用 --module=commonjs
(默认设置)编译此代码,您将得到非常正常的 commonjs 代码:
"use strict";
var module1 = require('./module1');
var n = module1.f('z');
如果你的模块导出一些对象,例如像这样
module2.js
:module.exports = { a: 'a', n: 1 };
最好避免使用 es6 语法导入它 - es6 始终假定模块导出命名空间,而不是对象。在打字稿中,有一种叫做 import require 的特殊语法:
import m2 = require('./module2');
let n1: string = m2.a;
声明文件,module2.d.ts
,必须使用export assignment
语法:
export = {} as {a: string, n: number};
实际值 {}
在声明文件中无关紧要,可以是任何东西 - 只有类型很重要(作为未命名接口的类型转换给出)。
如果一个模块导出一个字符串,就像你的例子一样,声明可以像
一样简单export = '';