Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error when extending a class in typescript

Tags:

typescript

I saw this question which is similar :

Cross referencing with extended classes in TypeScipt

though i couldnt figure out what was wrong still. My problem is that i have a class gameobject that i want to extend but as long as the sprite class that entends gameobject exists i get an error

module Game {
export class GameObject {
    x = 0;
    y = 0;
    W = 0;
    H = 0;
    img = new Image();
    scale = 0;

    constructor(img, x, y, w, h, scale?) {
        this.img = img;
        this.x = x || 0;
        this.y = y || 0;
        this.W = w;
        this.H = h;
        this.scale = scale || 1;
    }
    update() {

    }
    render(context, x, y) {
        context.drawImage(this.img, this.x, this.y, this.W, this.H, x, y, this.W * this.scale, this.H * this.scale);
    }
}
}

.

module Game {
export class Sprite extends GameObject {

}
}

I get the following error:

Uncaught TypeError: Cannot read property 'prototype' of undefined

Uncaught TypeError: undefined is not a function

According to the post at the top, it seems like i have a circular dependency and it it calling the sprite class first for some reason?

Thanks in advance.

like image 725
Shadow Avatar asked Feb 15 '14 03:02

Shadow


1 Answers

I found the error, apparently you cannot extend files normally when you are outputting the typescript code to one javascript file so you have to use ///reference like this:

///<reference path='gameobject.ts' />
module Game {
    export class Sprite extends GameObject {
    }
}

Post about it:

https://typescript.codeplex.com/workitem/1590

like image 135
Shadow Avatar answered Nov 09 '22 20:11

Shadow