2

I'm learning some typescript but I'm not entirely sure what the problem in my code is.

I'm trying to implement an event system to make my domain observable (similar to what C# has with events) in a generic way and it compiles without a hitch. It even shows the correct type inference in intellisense (for sender?) when I register a handler.

But when I try to run it I keep getting a "Uncaught TypeError: Cannot read property 'prototype' of undefined " in the __extends helper function.

This is my code:

module Pathing.Domain {

export interface IEventArgs {

}

export interface EventHandler<S,T extends IEventArgs> {
    (sender?: S, e?: T): void
}

export class StdEvent<S> extends EventDelegate<S, IEventArgs> {

}

export class EventDelegate<S, T> {
    private handlers: EventHandler<S,T>[] = [];

    public add(handler: EventHandler<S, T>): void {
        this.handlers.push(handler);
    }

    public remove(handler: EventHandler<S, T>): void {
        this.handlers = this.handlers.filter(h => h != handler);
    }

    public raise(...args: any[]): void {
        for (var i = 0; i < this.handlers.length; i++)
            this.handlers[i].call(this, args);
    }
}


}

I can also simulate it on the playground: http://www.typescriptlang.org/Playground#src=%20module%20Pathing.Domain%20%7B%0A%0A%20%20%20%20export%20interface%20IEventArgs%20%7B%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20export%20interface%20EventHandler%3CS%2CT%20extends%20IEventArgs%3E%20%7B%0A%20%20%20%20%20%20%20%20(sender%3F%3A%20S%2C%20e%3F%3A%20T)%3A%20void%0A%20%20%20%20%7D%0A%0A%20%20%20%20export%20class%20StdEvent%3CS%3E%20extends%20EventDelegate%3CS%2C%20IEventArgs%3E%20%7B%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20export%20class%20EventDelegate%3CS%2C%20T%3E%20%7B%0A%20%20%20%20%20%20%20%20private%20handlers%3A%20EventHandler%3CS%2CT%3E%5B%5D%20%3D%20%5B%5D%3B%0A%0A%20%20%20%20%20%20%20%20public%20add(handler%3A%20EventHandler%3CS%2C%20T%3E)%3A%20void%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20this.handlers.push(handler)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20remove(handler%3A%20EventHandler%3CS%2C%20T%3E)%3A%20void%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20this.handlers%20%3D%20this.handlers.filter(h%20%3D%3E%20h%20!%3D%20handler)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20raise(...args%3A%20any%5B%5D)%3A%20void%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20this.handlers.length%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20this.handlers%5Bi%5D.call(this%2C%20args)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A

I've got the same problem in Internet explorer (which I used for debugging in vs2013) and chrome.

Has anyone got a clue what might be wrong?

--

Edit: It looks like the culprit is export class StdEvent<S> extends EventDelegate<S, IEventArgs> {

Can anyone shed some light as why this results in a runtime error yet the compiler happily agrees with it?

1 Answer 1

8

The base class EventDelegate needs to be defined before the derived class StdEvent

export class EventDelegate {

}

export class StdEvent extends EventDelegate {
}

It's conceivable that in a future version the TypeScript compiler would take care of this for you, but until then you are responsible for ordering the definitions correctly.

3
  • I would also use the term "defined" (not declared) but doesn't matter.
    – basarat
    Jul 3, 2014 at 10:59
  • That was it! I can't believe I missed something so obvious, thanks a lot!
    – Drakarah
    Jul 3, 2014 at 11:02
  • @basarat yes, defined (not declared). Jul 3, 2014 at 12:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.