[IMP] component: display nice error for wrong child component

If you declare a child component which is not actually a Component,
the error message is not very friendly and not very helpfull to find
what happens and which child component is wrong.

```
const ChildComponent = "not a component constructor";

class MyComponent extends Component {
    static components = { ChildComponent };
}
```

This commit improves the type declaration for those working with Typescript
and adds a runtime check for javascript codebases
This commit is contained in:
Lucas Lefèvre
2022-05-06 16:39:07 +02:00
committed by Géry Debongnie
parent d09771b04b
commit 5c71744e19
4 changed files with 33 additions and 0 deletions
+1
View File
@@ -10,6 +10,7 @@ interface StaticComponentProperties {
template: string;
defaultProps?: any;
props?: any;
components?: { [componentName: string]: ComponentConstructor };
}
export type ComponentConstructor<P extends Props = any, E = any> = (new (
+2
View File
@@ -107,6 +107,8 @@ export function component<P extends object>(
C = parent.constructor.components[name as any];
if (!C) {
throw new Error(`Cannot find the definition of component "${name}"`);
} else if (!(C.prototype instanceof Component)) {
throw new Error(`"${name}" is not a Component. It must inherit from the Component class`);
}
}
node = new ComponentNode(C, props, ctx.app, ctx, key);