[IMP] types: correctly support Function type for props-validation

Previously, having a Function as a type in the static props description
of a component would only work if the component was not the root
component, as the static props description on Component was "any",
whereas the static props description on ComponentConstructor was
"Schema". This meant that static props description on non-root
components was not type-checked, and on root components it was
type-checked only on the mount call.

This commit makes it so that static type description is of type "Schema"
on Component, now causing static props description to be type-checked,
and adds `typeof Function` to the `BaseType` union, which allows
declaring that a component expects a function as a prop.

Closes #1448
This commit is contained in:
Samuel Degueldre
2023-11-22 08:37:24 +01:00
committed by Géry Debongnie
parent 941190dfa8
commit e94428a186
4 changed files with 5 additions and 4 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ export type ComponentConstructor<P extends Props = any, E = any> = (new (
export class Component<Props = any, Env = any> { export class Component<Props = any, Env = any> {
static template: string = ""; static template: string = "";
static props?: any; static props?: Schema;
static defaultProps?: any; static defaultProps?: any;
props: Props; props: Props;
+1 -1
View File
@@ -65,7 +65,7 @@ export class Portal extends Component {
type: String, type: String,
}, },
slots: true, slots: true,
}; } as const;
setup() { setup() {
const node: any = this.__owl__; const node: any = this.__owl__;
+1
View File
@@ -8,6 +8,7 @@ type BaseType =
| typeof Date | typeof Date
| typeof Object | typeof Object
| typeof Array | typeof Array
| typeof Function
| true | true
| "*"; | "*";
+2 -2
View File
@@ -594,7 +594,7 @@ describe("props validation", () => {
test("props: can be defined with a boolean", async () => { test("props: can be defined with a boolean", async () => {
class SubComp extends Component { class SubComp extends Component {
static props = { message: true }; static props = { message: true } as const;
} }
expect(() => { expect(() => {
validateProps(SubComp as any, {}); validateProps(SubComp as any, {});
@@ -636,7 +636,7 @@ describe("props validation", () => {
test("props: extra props cause an error, part 2", async () => { test("props: extra props cause an error, part 2", async () => {
class SubComp extends Component { class SubComp extends Component {
static props = { message: true }; static props = { message: true } as const;
} }
expect(() => { expect(() => {
validateProps(SubComp as any, { message: 1, flag: true }); validateProps(SubComp as any, { message: 1, flag: true });