0

我想在 NodeJS 环境中使用 TypeScript。由于我对 TypeScript 完全陌生,我不确定如何使用 NodeJS 模块系统正确扩展类。


我想ChampionGameObject.

游戏对象.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
4

1 回答 1

0

而不是使用module.exports = GameObject;with var/requireuseimport/require

这将为您在导入方面提供类型安全性。这些被称为文件模块并记录在这里:https ://basarat.gitbooks.io/typescript/content/docs/project/modules.html

于 2015-11-15T05:10:05.460 回答