feat: Support for generic props component#3682
Conversation
| PP | ||
| > = | ||
| // If props is a class we should ifnore all the process | ||
| (PropsOrPropOptions extends { prototype: ComponentOptionClass } |
There was a problem hiding this comment.
This is the most important part of this PR, this pretty much bypasses our ComponentPublicInstanceConstructor to prevent the Props type to not be modified
| PP | ||
| > = | ||
| // If props is a class we should ifnore all the process | ||
| (PropsOrPropOptions extends { prototype: ComponentPropsOverride } |
|
Hey @pikax , you have done a great job 👍, it looks simpler than expected, do we need to add some dts test cases for mixins/extends? |
Not sure, because it will ignore all the declare function expectType<T>(a: T): void;
type OnChange<ValueType, Clearable> = Clearable extends true
? (value: ValueType | null) => void
: (value: ValueType) => void
interface GenericProp<Clearable, ValueType> {
clearable?: Clearable
value?: ValueType
onChange?: OnChange<ValueType, Clearable>
}
declare function S<
Clearable extends boolean,
ValueType extends string | number | null | undefined
>(): {
$props: GenericProp<Clearable, ValueType>
}
S().$props // $props is not `GenericProp<Clearable, ValueType>`
class C<
Clearable extends boolean,
ValueType extends string | number | null | undefined
> {
$props = {} as GenericProp<Clearable, ValueType>
}
new C().$props // $props is not `GenericProp<Clearable, ValueType>`
// but `C` has the correct type
// This would also work
type VueProps<T extends new () => { $props: any }> = T
declare const Comp: VueProps<{
new <
Clearable extends boolean,
ValueType extends string | number | null | undefined
>(): { $props: GenericProp<Clearable, ValueType> }
}>The templated constructor intact to keep the correct I would say this is an advanced usage, with the current implementation it won't support Public API typing would also been lost, since the constructor will only return |
I agree with this point |
|
Must we create a new class for props definition? |
fix #3102
Allow generic components through a class component declaration.
Class maintain the correct type, if we use function or even the current DefineComponent the finer type will be lost.
This overload allows for the users to override the types to a finer grain
Open for better solutions
example