I am writing a simple ECS library, and would like to force my users to provide a name of the system they are defining. I want to use it in error / debugging messages. At the same time I would not like to force them to implement every system as a class.
What I would like to achieve is to allow the user to either provide an object literal that has the "name" property, or to provide an instance of a class. In the later case I do not need the name, as the console.log(instance) would be descriptive enough. As in:
class MyStatefulSystem implements System {
run(...) {...}
}
createEngine().defineSystem(new MyStatefulSystem());
createEngine().defineSystem({name: "simple system", run: (...)=> {}})
How do I define a type that will only match instances of any class? What I have already, with some simplification, is:
export interface System {
run: (...) => void;
}
interface ExplicitlyNamedSystem extends System {
name: string;
}
type SystemClass = ?
export type NamedSystem =
| ExplicitlyNamedSystem
| SystemClass;