TypeScript: Reference interface in class when using AMD

kpko

I set up everything so far to use TypeScript with requirejs. I tested my scenario with to classes and everything works fine but I fail when I try to use interface definitions.

IComponent.ts:

/// <reference path="Actor.ts" />

interface IComponent {
  owner: Actor

  init()
  update(delta: number)
  render()
}

Actor.ts:

/// <reference path="IComponent.ts" />

class Actor {
    private components: IComponent[] = []

    add(component: IComponent) {
        component.owner = this;
        this.components.push(component);
    }

    remove(component: IComponent) {
        var index = this.components.indexOf(component);
        this.components.splice(index, 1);
    }

    update(delta: number) {
        this.components.forEach(c => c.update(delta));
    }

    render() {
        this.components.forEach(c => c.render());
    }

    init() {
        this.components.forEach(c => c.init());
    }
}

export = Actor

I get an error in my IComponent.ts: The symbol 'Actor' could not be found. How can I reference my Actor class from my interface?

Fenton

As it currently stands you have a logical circular reference, Actor depends on IComponent and IComponent depends on Actor.

Your IComponent interface is little use without your Actor class, so my recommended solution would be to place the IComponent interface within your actor.ts file. This way, the dependency is self-sufficient.

export interface IComponent {
    owner: Actor;

    init();
    update(delta: number);
    render();
}

export class Actor {
    private components: IComponent[] = []

    add(component: IComponent) {
        component.owner = this;
        this.components.push(component);
    }

    remove(component: IComponent) {
        var index = this.components.indexOf(component);
        this.components.splice(index, 1);
    }

    update(delta: number) {
        this.components.forEach(c => c.update(delta));
    }

    render() {
        this.components.forEach(c => c.render());
    }

    init() {
        this.components.forEach(c => c.init());
    }
}

Alternatively, you could make IComponent depend on an abstraction of Actor rather than on the concrete class (in your case, perhaps an IActor. Now your Actor depends on IComponent, but that is the end of the chain.

export interface IComponent {
    owner: IActor;

    init();
    update(delta: number);
    render();
}

export interface IActor {
    add(component: IComponent);
    remove(component: IComponent);
    update(delta: number);
    render();
    init();
}

Hacky Fix:

This resolves the error in the IComponent.ts file, but not in a nice way.

import Actor = require('actor');

interface IComponent {
    owner: Actor;

    init();
    update(delta: number);
    render();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeScript: Reference interface in class when using AMD

From Dev

Avoid duplicate reference / import of external javascript library in Typescript when using AMD modules

From Dev

Avoid duplicate reference / import of external javascript library in Typescript when using AMD modules

From Java

When use a interface or class in Typescript

From Dev

Call interface method using reference of class

From Dev

undefined reference to vtable when using interface

From Dev

Typescript: how to switch off ambient declarations when using AMD

From Dev

How to define and consume enums in typescript when using AMD?

From Dev

Where does the "use strict" go when using TypeScript and AMD Modules

From Dev

Typescript: how to switch off ambient declarations when using AMD

From Dev

Invalid constructor reference when using local class?

From Dev

Inheritance leads to error when using reference class

From Dev

Using TypeScript With jQuery Without AMD?

From Dev

Using TypeScript With jQuery Without AMD?

From Dev

Accessing the java Object class methods using an interface reference

From Dev

Typescript reference extended class

From Dev

Interface not being applied when Error class is extended in TypeScript

From Dev

Writing Typescript interface for a Class

From Dev

TypeScript interface to describe class

From Dev

How to create an object instance of a typescript class from within plain js using AMD modules

From Dev

How to create an object instance of a typescript class from within plain js using AMD modules

From Dev

Typescript: Type of exported AMD class not found

From Dev

Any object can be assigned in interface reference variable when class will implement these interface

From Dev

How to refer to Typescript enum in d.ts file, when using AMD?

From Dev

Typescript using Omit on interface

From Dev

Export imported modules using Typescript AMD

From Dev

Export imported modules using Typescript AMD

From Dev

typescript parse json with class and interface

From Dev

TypeScript class with same name as interface

Related Related

  1. 1

    TypeScript: Reference interface in class when using AMD

  2. 2

    Avoid duplicate reference / import of external javascript library in Typescript when using AMD modules

  3. 3

    Avoid duplicate reference / import of external javascript library in Typescript when using AMD modules

  4. 4

    When use a interface or class in Typescript

  5. 5

    Call interface method using reference of class

  6. 6

    undefined reference to vtable when using interface

  7. 7

    Typescript: how to switch off ambient declarations when using AMD

  8. 8

    How to define and consume enums in typescript when using AMD?

  9. 9

    Where does the "use strict" go when using TypeScript and AMD Modules

  10. 10

    Typescript: how to switch off ambient declarations when using AMD

  11. 11

    Invalid constructor reference when using local class?

  12. 12

    Inheritance leads to error when using reference class

  13. 13

    Using TypeScript With jQuery Without AMD?

  14. 14

    Using TypeScript With jQuery Without AMD?

  15. 15

    Accessing the java Object class methods using an interface reference

  16. 16

    Typescript reference extended class

  17. 17

    Interface not being applied when Error class is extended in TypeScript

  18. 18

    Writing Typescript interface for a Class

  19. 19

    TypeScript interface to describe class

  20. 20

    How to create an object instance of a typescript class from within plain js using AMD modules

  21. 21

    How to create an object instance of a typescript class from within plain js using AMD modules

  22. 22

    Typescript: Type of exported AMD class not found

  23. 23

    Any object can be assigned in interface reference variable when class will implement these interface

  24. 24

    How to refer to Typescript enum in d.ts file, when using AMD?

  25. 25

    Typescript using Omit on interface

  26. 26

    Export imported modules using Typescript AMD

  27. 27

    Export imported modules using Typescript AMD

  28. 28

    typescript parse json with class and interface

  29. 29

    TypeScript class with same name as interface

HotTag

Archive