ecsyjs / ecsy

Entity Component System for javascript
https://ecsyjs.github.io/ecsy/
MIT License
1.11k stars 115 forks source link

TypeScript: improve ComponentConstructor type #242

Open DavidPeicho opened 4 years ago

DavidPeicho commented 4 years ago

Hi,

Right now, component constructor accepts only objects containing the same attributes as the component, and false to use schema or not:

export interface ComponentConstructor<C extends Component<any>> {
  schema: ComponentSchema;
  isComponent: true;
  new (props?: Partial<Omit<C, keyof Component<any>>> | false): C;
}

The problem is that the systems accept only the default ComponentConstructor here. When creating custom component that doesn't need any schema, the developer ends up stuck with a constructor like:

export class FrameComponent extends Component<FramingParameters> {

  public target: Nullable<Target>;

  public constructor(target?: Target) {
    super(target);
    this.target = target ?? null;
  }
  ...
}

Which would work only if:

  1. The props argument contains attributes of the class ({ target: ... } in this case)
  2. The props argument can be false

Fix

1. Make the system class accept more generic component constructor

2. Re-think the instantiation / reset of component

  1. Regarding the constructor accepting false as props, wouldn't it make more sense to completely disable the schema check using a static argument? I am not sure what would be the use case of disabling the props schema per instance.

  2. Would it be possible to allow to forward properties that aren't of this type?

Let me know what you think! Thanks