最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript Error Object prototype may only be an Object or null: undefined - Stack Overflow

programmeradmin0浏览0评论

Hey i've just started with TypeScript and i tried to copy a javascript to TypeScript and changed the stuff to be more Object oriented. Now i'm trying to start it with cmd and the ns-node mand.

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

This Error doesn't make any sense so i googled it and saw that the problem might be ethe importinhgs of the project. I've tried every soultion on diffrent sites and they didn't help.

So my Code:

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 ;
...
}

So what could be the problem? Thy for your help

EDIT:

    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;
 }
...}

And the 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());
}

Hey i've just started with TypeScript and i tried to copy a javascript to TypeScript and changed the stuff to be more Object oriented. Now i'm trying to start it with cmd and the ns-node mand.

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

This Error doesn't make any sense so i googled it and saw that the problem might be ethe importinhgs of the project. I've tried every soultion on diffrent sites and they didn't help.

So my Code:

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 ;
...
}

So what could be the problem? Thy for your help

EDIT:

    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;
 }
...}

And the 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());
}
Share Improve this question edited Aug 11, 2018 at 20:33 Jonas Re asked Aug 11, 2018 at 20:19 Jonas ReJonas Re 931 gold badge1 silver badge10 bronze badges 4
  • I'm guessing the error refers to the superclass Entity of Player. Try adding console.log(Entity); above export class Player and make sure it is defined. – Matt McCutchen Commented Aug 11, 2018 at 20:21
  • 1 @MattMcCutchen Wow your right ^^ Thy and how can i fix this issue? They are in the Same Folder so it should work with the ./ prefix – Jonas Re Commented Aug 11, 2018 at 20:23
  • Can you add the relevant part of Entity.ts to the question so I can try to reproduce the problem? – Matt McCutchen Commented Aug 11, 2018 at 20:24
  • @MattMcCutchen Ok i have edited the post with the Entity and Bullet class – Jonas Re Commented Aug 11, 2018 at 20:42
Add a ment  | 

1 Answer 1

Reset to default 6

You have cyclic imports. Depending on which file you start from, when ts-node reaches an import of a file that is already in the process of loading, it will proceed past that import even though the imported file is not finished loading. This is how you can get past the import { Entity } from './Entity'; and Entity can still be undefined.

If Entity.ts doesn't need to access the Player class at the time the file is loaded, try moving the import { Player } from './Player'; line to the bottom of Entity.ts.

发布评论

评论列表(0)

  1. 暂无评论