Skip to content

Commit 802b740

Browse files
mcm-odooged-odoo
authored andcommitted
[IMP] props: improve props types again
1 parent 465a76b commit 802b740

File tree

2 files changed

+69
-79
lines changed

2 files changed

+69
-79
lines changed

src/runtime/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export { Component } from "./component";
4040
export type { ComponentConstructor } from "./component";
4141
export { useComponent } from "./component_node";
4242
export { props } from "./props";
43-
export type { PropsValidation } from "./props";
43+
export type { GetProps, PropsValidation } from "./props";
4444
export { status } from "./status";
4545
export { proxy, markRaw, toRaw } from "./reactivity/proxy";
4646
export { untrack } from "./reactivity/computations";

src/runtime/props.ts

Lines changed: 68 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
import { OwlError } from "../common/owl_error";
2+
import type { Component } from "./component";
23
import { getCurrent } from "./component_node";
34
import { validateSchema } from "./validation";
45

5-
type ConstructorTypedPropsValidation<T = any> = new (...args: any[]) => T;
6+
type ConstructorTypedPropsValidation<T = any> = new (...args: any) => T;
67

78
type UnionTypedPropsValidation = ReadonlyArray<TypedPropsValidation>;
89

9-
interface SchemaTypedPropsValidation<O extends boolean = boolean> {
10-
optional?: O;
11-
validate?(value: any): boolean;
12-
}
10+
type OptionalSchemaTypedPropsValidation<O extends boolean> = {
11+
optional: O;
12+
};
1313

14-
interface TypeSchemaTypedPropsValidation<T = any, O extends boolean = boolean>
15-
extends SchemaTypedPropsValidation<O> {
16-
type: new (...args: any[]) => T;
17-
}
14+
type ValidableSchemaTypedPropsValidation = {
15+
validate(value: any): boolean;
16+
};
1817

19-
interface MapSchemaTypedPropsValidation<O extends boolean = boolean>
20-
extends TypeSchemaTypedPropsValidation<ObjectConstructor, O> {
18+
type TypeSchemaTypedPropsValidation<T> = {
19+
type: new (...args: any) => T;
20+
};
21+
22+
type MapSchemaTypedPropsValidation = {
23+
type: ObjectConstructor;
2124
shape: PropsValidation;
22-
}
25+
};
2326

24-
interface RecordSchemaTypedPropsValidation<O extends boolean = boolean>
25-
extends TypeSchemaTypedPropsValidation<ObjectConstructor, O> {
27+
type RecordSchemaTypedPropsValidation = {
28+
type: ObjectConstructor;
2629
values: TypedPropsValidation;
27-
}
30+
};
2831

29-
interface ArraySchemaTypedPropsValidation<O extends boolean = boolean>
30-
extends TypeSchemaTypedPropsValidation<ArrayConstructor, O> {
32+
type ArraySchemaTypedPropsValidation = {
33+
type: ArrayConstructor;
3134
element: TypedPropsValidation;
32-
}
35+
};
36+
37+
type SchemaTypedPropsValidation<T, O extends boolean> = {
38+
type?: new (...args: any) => T;
39+
optional?: O;
40+
validate?(value: T): boolean;
41+
shape?: PropsValidation;
42+
values?: TypedPropsValidation;
43+
element?: TypedPropsValidation;
44+
};
3345

3446
type ValueTypedPropsValidation<T = any> = {
3547
value: T;
@@ -39,90 +51,68 @@ type TypedPropsValidation =
3951
| true
4052
| ConstructorTypedPropsValidation
4153
| UnionTypedPropsValidation
42-
| SchemaTypedPropsValidation
43-
| TypeSchemaTypedPropsValidation
44-
| MapSchemaTypedPropsValidation
45-
| RecordSchemaTypedPropsValidation
46-
| ArraySchemaTypedPropsValidation
54+
| SchemaTypedPropsValidation<any, boolean>
4755
| ValueTypedPropsValidation;
4856

4957
export type RecordPropsValidation = Record<string, TypedPropsValidation>;
50-
5158
export type KeysPropsValidation = readonly string[];
5259

5360
export type PropsValidation = RecordPropsValidation | KeysPropsValidation;
5461

5562
//-----------------------------------------------------------------------------
5663

57-
type Optional<T, O> = O extends true ? T | undefined : T;
58-
59-
type ConvertConstructorTypedPropsValidation<V extends ConstructorTypedPropsValidation> =
60-
V extends ConstructorTypedPropsValidation<infer I> ? I : never;
61-
62-
type ConvertUnionTypedPropsValidation<V extends UnionTypedPropsValidation> = V[number];
63-
64-
type ConvertMapSchemaTypedPropsValidation<V extends MapSchemaTypedPropsValidation> =
65-
V extends MapSchemaTypedPropsValidation<infer O>
66-
? Optional<ConvertPropsValidation<V["shape"]>, O>
67-
: never;
68-
69-
type ConvertRecordSchemaTypedPropsValidation<V extends RecordSchemaTypedPropsValidation> =
70-
V extends RecordSchemaTypedPropsValidation<infer O>
71-
? Optional<{ [K: string]: ConvertTypedPropsValidation<V["values"]> }, O>
72-
: never;
73-
74-
type ConvertArraySchemaTypedPropsValidation<V extends ArraySchemaTypedPropsValidation> =
75-
V extends ArraySchemaTypedPropsValidation<infer O>
76-
? Optional<ConvertTypedPropsValidation<V["element"]>[], O>
77-
: never;
78-
79-
type ConvertTypeSchemaTypedPropsValidation<V extends TypeSchemaTypedPropsValidation> =
80-
V extends TypeSchemaTypedPropsValidation<infer I, infer O> ? Optional<I, O> : never;
81-
82-
type ConvertSchemaTypedPropsValidation<V extends SchemaTypedPropsValidation> =
83-
V extends SchemaTypedPropsValidation ? any : never;
84-
85-
type ConvertValueTypedPropsValidation<V extends ValueTypedPropsValidation> =
86-
V extends ValueTypedPropsValidation<infer T> ? T : never;
87-
8864
type ConvertTypedPropsValidation<V extends TypedPropsValidation> = V extends true
8965
? any
90-
: V extends ConstructorTypedPropsValidation
91-
? ConvertConstructorTypedPropsValidation<V>
66+
: V extends ConstructorTypedPropsValidation<infer I>
67+
? I
9268
: V extends UnionTypedPropsValidation
93-
? ConvertUnionTypedPropsValidation<V>
69+
? V[number]
9470
: V extends MapSchemaTypedPropsValidation
95-
? ConvertMapSchemaTypedPropsValidation<V>
71+
? ConvertPropsValidation<V["shape"]>
9672
: V extends RecordSchemaTypedPropsValidation
97-
? ConvertRecordSchemaTypedPropsValidation<V>
73+
? Record<string, ConvertTypedPropsValidation<V["values"]>>
9874
: V extends ArraySchemaTypedPropsValidation
99-
? ConvertArraySchemaTypedPropsValidation<V>
100-
: V extends TypeSchemaTypedPropsValidation
101-
? ConvertTypeSchemaTypedPropsValidation<V>
102-
: V extends SchemaTypedPropsValidation
103-
? ConvertSchemaTypedPropsValidation<V>
104-
: V extends ValueTypedPropsValidation
105-
? ConvertValueTypedPropsValidation<V>
75+
? ConvertTypedPropsValidation<V["element"]>[]
76+
: V extends TypeSchemaTypedPropsValidation<infer I>
77+
? I
78+
: V extends ValueTypedPropsValidation<infer T>
79+
? T
80+
: V extends OptionalSchemaTypedPropsValidation<boolean>
81+
? any
82+
: V extends ValidableSchemaTypedPropsValidation
83+
? any
10684
: never;
10785

108-
type ConvertRecordPropsValidation<V extends RecordPropsValidation> = {
109-
[K in keyof V]: ConvertTypedPropsValidation<V[K]>;
110-
};
111-
112-
type ConvertKeysPropsValidation<V extends KeysPropsValidation> = { [K in V[number]]: any };
113-
11486
type ConvertPropsValidation<V extends PropsValidation> = V extends KeysPropsValidation
115-
? ConvertKeysPropsValidation<V>
87+
? { [K in V[number] as K extends `${infer N}?` ? N : never]?: any } & {
88+
[K in V[number] as K extends `${string}?` ? never : K]: any;
89+
}
11690
: V extends RecordPropsValidation
117-
? ConvertRecordPropsValidation<V>
91+
? {
92+
[K in keyof V as V[K] extends OptionalSchemaTypedPropsValidation<true>
93+
? K
94+
: never]?: ConvertTypedPropsValidation<V[K]>;
95+
} & {
96+
[K in keyof V as V[K] extends OptionalSchemaTypedPropsValidation<true>
97+
? never
98+
: K]: ConvertTypedPropsValidation<V[K]>;
99+
}
118100
: never;
119101

120102
//-----------------------------------------------------------------------------
121103

122-
type Props<T extends Record<string, any>, V extends PropsValidation> = T &
123-
ConvertPropsValidation<V>;
104+
declare const isProps: unique symbol;
105+
type IsPropsObj = { [isProps]: true };
106+
export type Props<T, V extends PropsValidation> = IsPropsObj &
107+
(unknown extends T ? ConvertPropsValidation<V> : T);
108+
109+
export type GetProps<T extends Component> = {
110+
[K in keyof T]: T[K] extends IsPropsObj ? (x: Omit<T[K], typeof isProps>) => void : never;
111+
}[keyof T] extends (x: infer I) => void
112+
? { [K in keyof I]: I[K] }
113+
: never;
124114

125-
export function props<T extends Record<string, any>, V extends PropsValidation = PropsValidation>(
115+
export function props<T = unknown, V extends PropsValidation = PropsValidation>(
126116
validation?: V
127117
): Props<T, V> {
128118
const node = getCurrent();

0 commit comments

Comments
 (0)