diff --git a/src/component.ts b/src/component.ts index d46d834c..638a96a1 100644 --- a/src/component.ts +++ b/src/component.ts @@ -48,7 +48,7 @@ export interface Type extends Function { export class Component< T extends Env, - Props, + Props extends {}, State extends {} > extends EventBus { readonly __widget__: Meta; @@ -96,7 +96,7 @@ export class Component< // Pro: if props is empty, we can create easily a widget // Con: this is not really safe // Pro: but creating widget (by a template) is always unsafe anyway - this.props = props; + this.props = props || {}; let id: number = getId(); let p: Component | null = null; if (parent instanceof Component) { diff --git a/tests/component.test.ts b/tests/component.test.ts index d15c43ef..b9c21c5e 100644 --- a/tests/component.test.ts +++ b/tests/component.test.ts @@ -70,6 +70,12 @@ class WidgetB extends Widget { //------------------------------------------------------------------------------ describe("basic widget properties", () => { + test("props and state are properly defined", async () => { + const widget = new Widget(env); + expect(widget.props).toEqual({}); + expect(widget.state).toEqual({}); + }); + test("has no el after creation", async () => { const widget = new Widget(env); expect(widget.el).toBe(null);