TypeScript错误对象原型只能是一个对象或null:未定义

时间:2018-08-11 20:19:17

标签: javascript typescript import

嘿,我刚开始使用TypeScript,我试图将JavaScript复制到TypeScript,并将内容更改为更面向对象。 现在,我尝试使用cmd和ns-node命令启动它。

private username:string;
           ^
TypeError: Object prototype may only be an Object or null: undefined

此错误没有任何意义,所以我在Google上搜索了一下,发现问题可能出在该项目的导入上。我已经尝试了在不同站点上进行的所有尝试,但它们并没有帮助。

所以我的代码:

import { Entity } from './Entity';
import { Bullet } from './Bullet';

import * as fs from "fs";
export class Player extends Entity{

private username:string;
protected pressingRight:boolean ;
protected pressingLeft:boolean ;
...
}

那么可能是什么问题?您的帮助

编辑:

    import {Point} from "./Point";
    import {Bullet} from "./Bullet";
import {Player} from "./Player";
export class Entity{

protected x:number;
protected y:number;
protected spdX:number = 0;
protected spdY:number = 0;
protected id:number;
protected map:string;


protected static initPack = {player:[],bullet:[]};
protected static removePack = {player:[],bullet:[]};

 constructor(x:number,y:number,id:number,map:string){
    this.x = x;
    this.y=y;
    this.id=id;
    this.map=map;
 }
...}

还有Bullet.ts:

    import {Entity} from './Entity';
import {Player} from './Player';
export class Bullet extends Entity{

private angle:number;
private parentID:number;
private timer:number = 0;
private toRemove:boolean=false;

public static list={};

constructor(x:number,y:number,map:string,parentId:number,angle:number,id:number){
    super(x,y,id,map);
    this.angle = angle;
    this.spdX= Math.cos(angle/180*Math.PI) * 10;
    this.spdY= Math.sin(angle/180*Math.PI) * 10;
    this.parentID = parentId;

    Bullet.list[this.id] = self;
    Bullet.initPack.bullet.push(this.getInitPack());
}

1 个答案:

答案 0 :(得分:0)

您有周期性进口。取决于您从哪个文件开始,当ts-node到达已在加载过程中的文件的导入时,即使导入的文件尚未完成加载,它也会继续进行该导入。这样可以克服import { Entity } from './Entity';,而Entity仍然是不确定的。

如果Entity.ts在加载文件时不需要访问Player类,请尝试将import { Player } from './Player';行移到Entity.ts的底部。 / p>