From e94428a1867f769eba2f0e647d93297643b7cdce Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Wed, 22 Nov 2023 08:37:24 +0100 Subject: [PATCH] [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 --- src/runtime/component.ts | 2 +- src/runtime/portal.ts | 2 +- src/runtime/validation.ts | 1 + tests/components/props_validation.test.ts | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/runtime/component.ts b/src/runtime/component.ts index 61a025ac..6e4bbd92 100644 --- a/src/runtime/component.ts +++ b/src/runtime/component.ts @@ -23,7 +23,7 @@ export type ComponentConstructor

= (new ( export class Component { static template: string = ""; - static props?: any; + static props?: Schema; static defaultProps?: any; props: Props; diff --git a/src/runtime/portal.ts b/src/runtime/portal.ts index 72d40538..5476e340 100644 --- a/src/runtime/portal.ts +++ b/src/runtime/portal.ts @@ -65,7 +65,7 @@ export class Portal extends Component { type: String, }, slots: true, - }; + } as const; setup() { const node: any = this.__owl__; diff --git a/src/runtime/validation.ts b/src/runtime/validation.ts index 29063fd4..36ed9f3e 100644 --- a/src/runtime/validation.ts +++ b/src/runtime/validation.ts @@ -8,6 +8,7 @@ type BaseType = | typeof Date | typeof Object | typeof Array + | typeof Function | true | "*"; diff --git a/tests/components/props_validation.test.ts b/tests/components/props_validation.test.ts index 265a36ba..f5f744b0 100644 --- a/tests/components/props_validation.test.ts +++ b/tests/components/props_validation.test.ts @@ -594,7 +594,7 @@ describe("props validation", () => { test("props: can be defined with a boolean", async () => { class SubComp extends Component { - static props = { message: true }; + static props = { message: true } as const; } expect(() => { validateProps(SubComp as any, {}); @@ -636,7 +636,7 @@ describe("props validation", () => { test("props: extra props cause an error, part 2", async () => { class SubComp extends Component { - static props = { message: true }; + static props = { message: true } as const; } expect(() => { validateProps(SubComp as any, { message: 1, flag: true });