Typescript 内部模块 - 无法在同一模块中引用导出的 class
Typescript internal modules - unable to reference exported class within same module
嗨!
首先,很抱歉,如果之前有人问过这个问题,我尝试了一些谷歌搜索,但没有找到任何东西(至少我认为没有找到相同的问题)。我是 Typescript 的新手,所以还在掌握!
我正在制作一个小游戏作为项目来复习我的 JavaScript,并决定使用 Typescript,因为它似乎非常适合模块化我的应用程序。全部 运行 在节点服务器上 - 如果需要,我可以提供版本号!
我有两个文件,player.ts 和 scene.ts,它们属于同一个模块(因为它们是我程序的一部分,但为了模块化,我想将它们分开):
player.ts
module TheGame {
export class Player {
name: string;
constructor(name: string) {
this.name = name;
}
}
}
scene.ts
///<reference path="player.ts"/>
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three");
module TheGame {
export class Scene {
player: any;
constructor() {
this.player = new Player('test');
}
//Use of Three throughout file
}
}
这两个文件都在同一个文件夹中,当我尝试编译它们时:
tsc player.ts scene.ts --module commonjs
我收到错误:
testscene.ts(10,22): error TS2095: Could not find symbol 'Player'.
如果我删除对 Three 的引用和导入,这编译得很好,但显然我需要在 class 中使用 Three。 (播放器中也需要三个class,以后可能会更多)即使有错误,它似乎实际上编译了JS文件,我不确定为什么。
我不明白这是如何工作的?
使用外部模块和内部模块是否有一些限制?
模块?
有没有办法导入所有外部 classes 我的应用程序
在它自己的 TS 文件中需要(三,socket.io,等等)来完成这项工作,
如果这是一个解决方案?
提前致谢!
已回答:
解决方案是,使用 commonJS,您需要显式导出和导入,并且使用模块并没有真正意义(感谢@ssube 和@BGR 的解决方案)。
工作代码:
player.ts
export class Player {
name: string;
constructor(name: string) {
this.name = name;
}
}
export = Player;
scene.ts
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three");
import Player = require("./player");
class Scene {
player: Player;
constructor() {
this.player = new Player('test');
}
//Use of Three throughout file
}
export = Scene;
如果你想使用 commonjs(我认为你应该)你需要显式导出和导入 try
player.ts
class Player {
name: string;
constructor(name: string) {
this.name = name;
}
}
export = Player
scene.ts
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three"); //I guess 'jquery' is a typo
import Player = require('./Player')
class Scene {
player: any; //any -> Player ?
constructor() {
this.player = new Player('test');
}
//Use of Three throughout file
}
export = Scene //if this makes sense
使用包装器 modules
(如 TheGame
)在使用 commonJS 时没有真正意义(事物是孤立的)
您不需要引用 .ts 文件
由于您已将它们定义为模块,因此 Player
对 TheGame
不可见,即使它们在同一个文件中也是如此。这就是模块的目的和力量。
您需要在 scene.ts
内从 player.ts
import Player
使其在该范围内可见。根据您的输出,这可能会转化为 AMD 或 CommonJS 要求。
引用文件使编译知道定义并允许验证,但实际上并没有将这些符号拉入当前范围并使它们可用。
我建议将 Player
作为其文件的默认导出,以简化导入:
///<reference path="player.ts"/>
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three");
import Player = require("player");
嗨!
首先,很抱歉,如果之前有人问过这个问题,我尝试了一些谷歌搜索,但没有找到任何东西(至少我认为没有找到相同的问题)。我是 Typescript 的新手,所以还在掌握!
我正在制作一个小游戏作为项目来复习我的 JavaScript,并决定使用 Typescript,因为它似乎非常适合模块化我的应用程序。全部 运行 在节点服务器上 - 如果需要,我可以提供版本号!
我有两个文件,player.ts 和 scene.ts,它们属于同一个模块(因为它们是我程序的一部分,但为了模块化,我想将它们分开):
player.ts
module TheGame {
export class Player {
name: string;
constructor(name: string) {
this.name = name;
}
}
}
scene.ts
///<reference path="player.ts"/>
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three");
module TheGame {
export class Scene {
player: any;
constructor() {
this.player = new Player('test');
}
//Use of Three throughout file
}
}
这两个文件都在同一个文件夹中,当我尝试编译它们时:
tsc player.ts scene.ts --module commonjs
我收到错误:
testscene.ts(10,22): error TS2095: Could not find symbol 'Player'.
如果我删除对 Three 的引用和导入,这编译得很好,但显然我需要在 class 中使用 Three。 (播放器中也需要三个class,以后可能会更多)即使有错误,它似乎实际上编译了JS文件,我不确定为什么。
我不明白这是如何工作的?
使用外部模块和内部模块是否有一些限制? 模块?
有没有办法导入所有外部 classes 我的应用程序 在它自己的 TS 文件中需要(三,socket.io,等等)来完成这项工作, 如果这是一个解决方案?
提前致谢!
已回答:
解决方案是,使用 commonJS,您需要显式导出和导入,并且使用模块并没有真正意义(感谢@ssube 和@BGR 的解决方案)。
工作代码:
player.ts
export class Player {
name: string;
constructor(name: string) {
this.name = name;
}
}
export = Player;
scene.ts
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three");
import Player = require("./player");
class Scene {
player: Player;
constructor() {
this.player = new Player('test');
}
//Use of Three throughout file
}
export = Scene;
如果你想使用 commonjs(我认为你应该)你需要显式导出和导入 try
player.ts
class Player {
name: string;
constructor(name: string) {
this.name = name;
}
}
export = Player
scene.ts
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three"); //I guess 'jquery' is a typo
import Player = require('./Player')
class Scene {
player: any; //any -> Player ?
constructor() {
this.player = new Player('test');
}
//Use of Three throughout file
}
export = Scene //if this makes sense
使用包装器
modules
(如TheGame
)在使用 commonJS 时没有真正意义(事物是孤立的)您不需要引用 .ts 文件
由于您已将它们定义为模块,因此 Player
对 TheGame
不可见,即使它们在同一个文件中也是如此。这就是模块的目的和力量。
您需要在 scene.ts
内从 player.ts
import Player
使其在该范围内可见。根据您的输出,这可能会转化为 AMD 或 CommonJS 要求。
引用文件使编译知道定义并允许验证,但实际上并没有将这些符号拉入当前范围并使它们可用。
我建议将 Player
作为其文件的默认导出,以简化导入:
///<reference path="player.ts"/>
///<reference path="../typings/threejs/three.d.ts"/>
import jquery = require("three");
import Player = require("player");