Javascript 如何在Nodejs REPL中使用Typescript类?
我创建了一个Javascript 如何在Nodejs REPL中使用Typescript类?,javascript,node.js,typescript,Javascript,Node.js,Typescript,我创建了一个foo.ts如下: class Foo{ public echo(){ console.log("foo"); } } var Foo = (function () { function Foo() { } Foo.prototype.echo = function () { console.log("foo"); }; return Foo; })(); $ node > require('.
foo.ts
如下:
class Foo{
public echo(){
console.log("foo");
}
}
var Foo = (function () {
function Foo() {
}
Foo.prototype.echo = function () {
console.log("foo");
};
return Foo;
})();
$ node
> require('./foo.js');
{}
> f = new Foo
ReferenceError: Foo is not defined
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
它输出如下javascript代码:
class Foo{
public echo(){
console.log("foo");
}
}
var Foo = (function () {
function Foo() {
}
Foo.prototype.echo = function () {
console.log("foo");
};
return Foo;
})();
$ node
> require('./foo.js');
{}
> f = new Foo
ReferenceError: Foo is not defined
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
我想调用nodejs REPL中的echo
函数,但它会导致如下错误:
class Foo{
public echo(){
console.log("foo");
}
}
var Foo = (function () {
function Foo() {
}
Foo.prototype.echo = function () {
console.log("foo");
};
return Foo;
})();
$ node
> require('./foo.js');
{}
> f = new Foo
ReferenceError: Foo is not defined
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
$node
>要求('./foo.js');
{}
>f=新Foo
ReferenceError:未定义Foo
回复:1:10
在REPLServer.self.eval(repl.js:110:21)
在接口处。(回复js:239:12)
位于Interface.EventEmitter.emit(events.js:95:17)
在接口处在线(readline.js:202:10)
在接口处。\u行(readline.js:531:8)
在接口处写入(readline.js:760:14)
在ReadStream.onkeypress(readline.js:99:10)
在ReadStream.EventEmitter.emit(events.js:98:17)
在emitKey(readline.js:1095:12)
如何实例化类并调用函数
echo
?Node.js没有像browserwindow
对象那样的泄漏全局变量
要在node.js中使用TypeScript代码,需要使用commonjs并导出类,即
class Foo{
public echo(){
console.log("foo");
}
}
export = Foo;
然后在REPL中:
$ node
> var Foo = require('./foo.js');
{}
> f = new Foo();
要了解更多有关AMD/CommonJS的信息,请执行以下操作:现代更新:您可以轻松使用ES模块: 在答复中:
$ node -r esm
> import { Foo } from './foo.js';
{}
> f = new Foo();
我不确定typescript是如何工作的,但很明显,您没有从Foo.js导出任何内容(也没有将所需的模块分配给任何内容)。可能先让自己熟悉任一节点的模块系统。