Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for generic props component #3682

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 65 additions & 31 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ComponentOptionsWithObjectProps,
ComponentOptionsMixin,
RenderFunction,
ComponentOptionsBase
ComponentOptionsBase,
ComponentPropsOverride
} from './componentOptions'
import {
SetupContext,
Expand All @@ -16,7 +17,8 @@ import {
import {
ExtractPropTypes,
ComponentPropsOptions,
ExtractDefaultPropTypes
ExtractDefaultPropTypes,
PropType
} from './componentProps'
import { EmitsOptions } from './componentEmits'
import { isFunction } from '@vue/shared'
Expand All @@ -43,35 +45,39 @@ export type DefineComponent<
PP = PublicProps,
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
PP & Props,
Defaults,
true
> &
Props
> &
ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Defaults
> &
PP
> =
// If props is a class we should ifnore all the process
(PropsOrPropOptions extends { prototype: ComponentPropsOverride }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo :)

? PropsOrPropOptions
: ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
PP & Props,
Defaults,
true
> &
Props
>) &
ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Defaults
> &
PP

// defineComponent is a utility that is primarily used for type inference
// when declaring components. Type inference is provided in the component
Expand Down Expand Up @@ -179,6 +185,34 @@ export function defineComponent<
>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>

// overload 5: Allow overriding Props object
export function defineComponent<
O extends { prototype: ComponentPropsOverride },
RawBindings = {},
D = {},
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string,
PP = PublicProps
>(
options: ComponentOptionsWithObjectProps<
O extends new () => { $props: infer P }
? { [K in keyof P]: PropType<P[K]> }
: {},
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
): DefineComponent<O, RawBindings, D, C, M, Mixin, Extends, E, EE, PP>

// implementation, close to no-op
export function defineComponent(options: unknown) {
return isFunction(options) ? { setup: options, name: options.name } : options
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ export interface ComponentOptionsBase<
__defaults?: Defaults
}

export abstract class ComponentPropsOverride<Props = {}> {
readonly $props: Props = {} as Props
}

export type ComponentOptionsWithoutProps<
Props = {},
RawBindings = {},
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export {
ComponentOptionsWithArrayProps,
ComponentCustomOptions,
ComponentOptionsBase,
ComponentPropsOverride,
RenderFunction,
MethodOptions,
ComputedOptions
Expand Down
33 changes: 33 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ComponentOptions,
SetupContext,
IsUnion,
ComponentPropsOverride,
h
} from './index'

Expand Down Expand Up @@ -1038,6 +1039,38 @@ describe('async setup', () => {
vm.a = 2
})

// #3102
describe('Generic props', () => {
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>
}

class CompProps<
Clearable extends boolean,
ValueType extends string | number | null | undefined
> extends ComponentPropsOverride<GenericProp<Clearable, ValueType>> {}

const Comp = defineComponent<typeof CompProps>({
props: {
value: Object,
clearable: Boolean
}
})
;<Comp
value={'sss'}
clearable
onChange={a => {
expectType<'sss' | null>(a)
}}
/>
})

// check if defineComponent can be exported
export default {
// function components
Expand Down
34 changes: 33 additions & 1 deletion test-dts/h.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
Component,
expectError,
expectAssignable,
resolveComponent
resolveComponent,
ComponentPropsOverride
} from './index'

describe('h inference w/ element', () => {
Expand Down Expand Up @@ -233,3 +234,34 @@ describe('resolveComponent should work', () => {
message: '1'
})
})

// #3102
describe('Class component generic props', () => {
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>
}

class CompProps<
Clearable extends boolean,
ValueType extends string | number | null | undefined
> extends ComponentPropsOverride<GenericProp<Clearable, ValueType>> {}

const Comp = defineComponent<typeof CompProps>({
props: {},
setup() {
return {}
}
})

h(Comp, {
clearable: true,
value: 'test',
onChange(e) {}
})
})