Is this good or bad practice, alternatives and tricks ? (modules, classes, dictionary)

Topics: General
Apr 2, 2013 at 10:57 PM
Edited Apr 3, 2013 at 10:44 PM
My main topic is... this works but is it good ?


we've organized our code this way:
export module
     export class Dictionary
and then in some other class:
import MDictionary = module("tools/Dictionary");
export class Dictionary extends {}
and i can use this:
private _components: Dictionary;
this._components = new Dictionary();
instead of this:
private _components:;
this._components = new;

I want a method to take as parameter, a random Class object as:
Entity = new Entity();
Entity .add( new Position( 100, 200 ));
public add( component: () => any, componentClass  = null ): Entity
   if ( !componentClass )
    componentClass = component.prototype;
    if ( this._components.has(componentClass))
    this._components.add(componentClass, component);
    this.componentAdded.dispatch( this, componentClass );
    return this;
So is this type "() => any" good enough and is ".prototype" a right way to do this.
Apr 3, 2013 at 3:38 PM
We're planning to introduce a new feature in 0.9.x that should help with the first sample, based on the technique of doing "export =". It would allow you to do (syntax subject to change):
//file A.ts
class Dictionary
export = Dictionary

//file B.ts
import "A" as Dict;
var d = new Dict();
I'm not clear on what problem you're solving in the second example.
Apr 3, 2013 at 10:16 PM
Tnx, for the response. Cool news for the import.

About sample 2, my uncertainty comes that I'm not sure that "component: () => any" is the right way
to check if the component is class instance (aka function) and I've added the whole code part as example
cause, i want to have a dictionary(hashmap) with (Class, Component) where Component is the object, and Class is... well the Class (in JS the prototype).

Maybe my second sample is not so TS oriented, but more of code structure and implementation.