Skip to content

Commit

Permalink
[IMP] types: correctly support Function type for props-validation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sdegueldre authored and ged-odoo committed Dec 4, 2023
1 parent 941190d commit e94428a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ComponentConstructor<P extends Props = any, E = any> = (new (

export class Component<Props = any, Env = any> {
static template: string = "";
static props?: any;
static props?: Schema;
static defaultProps?: any;

props: Props;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Portal extends Component {
type: String,
},
slots: true,
};
} as const;

setup() {
const node: any = this.__owl__;
Expand Down
1 change: 1 addition & 0 deletions src/runtime/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type BaseType =
| typeof Date
| typeof Object
| typeof Array
| typeof Function
| true
| "*";

Expand Down
4 changes: 2 additions & 2 deletions tests/components/props_validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit e94428a

Please sign in to comment.