5

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.

4
  • Can't seem to reproduce the error. What version of TS? Commented Feb 15, 2014 at 5:49
  • typescript 0.9.5, I have no idea what is causing this because even if i dont call the classes above, as long as they are in the project it gives that error. Commented Feb 15, 2014 at 6:09
  • Try restart Visual Studio... Commented Feb 15, 2014 at 8:51
  • Yes, I have tried that but nothing still sadly. Commented Feb 15, 2014 at 13:55

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that is one of the code organization patterns available to you. The other is amd for the browser : youtube.com/watch?v=KDrWLMUY0R0&hd=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.