我想在 NodeJS 环境中使用 TypeScript。由于我对 TypeScript 完全陌生,我不确定如何使用 NodeJS 模块系统正确扩展类。
我想Champion
用GameObject
.
游戏对象.ts
///<reference path="../../typings/node/node.d.ts"/>
class GameObject {
id:number;
name:string;
}
module.exports = GameObject;
冠军.ts
///<reference path="../../typings/node/node.d.ts"/>
///<reference path="Image.ts"/>
///<reference path="GameObject.ts"/>
class Champion extends GameObject {
// ...
}
module.exports = Champion;
到目前为止,这不会引发编译错误。
现在我想创建我的 Champion 的一个实例。这是我尝试过的
// I tried referencing the Champion.ts which haven't changed anything
var Champion = require('../api/types/Champion');
var c = new Champion();
我Champion.js
现在抛出以下错误:
ReferenceError:未定义游戏对象
所以我认为我需要require('GameObject')
在Champion.ts
其中运行我的应用程序。但我得到另一个错误。
要么我参考和require
我的GameObject
///<reference path="GameObject.ts"/>
var GameObject = require('./GameObject');
class Champion extends GameObject {
这给了我错误
重复标识符游戏对象
或者我只是require
得到
类型 any 不是构造函数类型
打字稿版本
$ tsc -v
message TS6029: Version 1.6.2