class GameObject { id:number; nam">
我想在TypeScript环境中使用NodeJS。由于我对TypeScript完全陌生,所以我不知道如何用NodeJS模块系统正确地扩展类。
我想用Champion
扩展我的类GameObject
。
GameObject.ts
///<reference path="../../typings/node/node.d.ts"/>
class GameObject {
id:number;
name:string;
}
module.exports = GameObject;
Champion.ts
///<reference path="../../typings/node/node.d.ts"/>
///<reference path="Image.ts"/>
///<reference path="GameObject.ts"/>
class Champion extends GameObject {
// ...
}
module.exports = 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:未定义GameObject
因此,我认为我需要在require('GameObject')
中使用Champion.ts
,这使我的应用程序能够运行。但我又犯了一个错误。
我引用和require
我的GameObject
///<reference path="GameObject.ts"/>
var GameObject = require('./GameObject');
class Champion extends GameObject {
这给了我错误
重复标识符GameObject
或者我只是require
然后
类型any不是构造函数类型。
TypeScript版本
$ tsc -v
message TS6029: Version 1.6.2
发布于 2015-11-14 21:10:05
不要将module.exports = GameObject;
与var/require
一起使用,而是使用import/require
这将给您输入端的类型安全性。这些被称为文件模块,并在这里文档化:https://basarat.gitbooks.io/typescript/content/docs/project/modules.html
https://stackoverflow.com/questions/33716457
复制