Skip to content

feat: Support for generic props component #3682

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions packages/runtime-core/__tests__/apiClass.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
h,
nodeOps,
render,
serializeInner,
triggerEvent,
TestElement,
nextTick,
ref,
defineComponent,
ComponentOptionClass
} from '@vue/runtime-test'

describe('api: VCA class', () => {
test('data', async () => {
const CXXX = class extends ComponentOptionClass {
setup() {
const foo = ref(1)
return () =>
h(
'div',
{
onClick: () => {
foo.value++
}
},
foo.value
)
}
}
const Comp = defineComponent(CXXX)
const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(serializeInner(root)).toBe(`<div>1</div>`)

triggerEvent(root.children[0] as TestElement, 'click')
await nextTick()
expect(serializeInner(root)).toBe(`<div>2</div>`)
})
})
114 changes: 83 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,
ComponentOptionClass
} from './componentOptions'
import {
SetupContext,
Expand Down Expand Up @@ -43,41 +44,79 @@ 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: ComponentOptionClass }
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the most important part of this PR, this pretty much bypasses our ComponentPublicInstanceConstructor to prevent the Props type to not be modified

? 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
// options (provided as the argument). The returned value has artificial types
// for TSX / manual render function / IDE support.

// export function defineComponent<O extends ComponentOptionClass>(
// // Props = {},
// // RawBindings = {},
// // D = {},
// // C extends ComputedOptions = {},
// // M extends MethodOptions = {},
// // Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
// // Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
// // E extends EmitsOptions = EmitsOptions,
// // EE extends string = string
// options: O
// ): DefineComponent<Props>

// WORKING
// export function defineComponent<
// C extends { prototype: B },
// B extends { prototype: C } = any
// >(o: C): C

// // kind working
// export function defineComponent<O extends { prototype: ComponentOptionClass }>(
// o: O
// ): DefineComponent<O>

// export function defineComponent<O extends ComponentOptionClass>(
// o: any
// ): DefineComponent<O>

// export function defineComponent<TClass extends ComponentOptionClass>(

// ) {

// }

// overload 1: direct setup function
// (uses user defined props interface)
export function defineComponent<Props, RawBindings = object>(
Expand Down Expand Up @@ -179,7 +218,20 @@ export function defineComponent<
>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>

// overload 4: ComponentOptionClass format to allow more powerful generics
export function defineComponent<O extends { prototype: ComponentOptionClass }>(
o: O
): DefineComponent<
O,
O extends { setup: (...a: any[]) => infer Setup } ? Setup : false
>

// implementation, close to no-op
export function defineComponent(options: unknown) {
return isFunction(options) ? { setup: options, name: options.name } : options
return isFunction(options)
? !options.prototype
? { setup: options, name: options.name }
: // @ts-expect-error
new options()
: options
}
30 changes: 28 additions & 2 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ import {
import {
ComponentObjectPropsOptions,
ExtractPropTypes,
ExtractDefaultPropTypes
ExtractDefaultPropTypes,
PropType
} from './componentProps'
import { EmitsOptions } from './componentEmits'
import { EmitFn, EmitsOptions } from './componentEmits'
import { Directive } from './directives'
import {
CreateComponentPublicInstance,
Expand Down Expand Up @@ -190,6 +191,31 @@ export interface ComponentOptionsBase<
__defaults?: Defaults
}

export abstract class ComponentOptionClass<
Props = {},
E extends EmitsOptions = EmitsOptions
> {
readonly __vccOpts = {}
readonly $props: Props = {} as Props
readonly $emit: EmitFn<E> = {} as EmitFn<E>

props?: { [K in keyof Props]: PropType<Props[K]> }
emits?: E

constructor() {
this.setup.bind(this)
}

setup(
props: Props,
ctx: SetupContext<E>
):
| Promise<Record<string, any>>
| Record<string, any>
| RenderFunction
| void {}
}

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,
ComponentOptionClass,
RenderFunction,
MethodOptions,
ComputedOptions
Expand Down
34 changes: 34 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
describe,
Component,
ConcreteComponent,
defineComponent,
PropType,
ref,
Expand All @@ -12,6 +13,7 @@ import {
ComponentOptions,
SetupContext,
IsUnion,
ComponentOptionClass,
h
} from './index'

Expand Down Expand Up @@ -1038,6 +1040,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>
}

const Comp = defineComponent(
class<
Clearable extends boolean,
ValueType extends string | number | null | undefined
> extends ComponentOptionClass<GenericProp<Clearable, ValueType>> {
props = {}
setup() {
return {}
}
}
)
;<Comp
value={'sss'}
clearable
onChange={a => {
expectType<'sss' | null>(a)
}}
/>
})

// check if defineComponent can be exported
export default {
// function components
Expand Down
32 changes: 32 additions & 0 deletions test-dts/h.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ComponentOptionClass } from '@vue/runtime-core'
import {
describe,
h,
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>
}

const Comp = defineComponent(
class<
Clearable extends boolean,
ValueType extends string | number | null | undefined
> extends ComponentOptionClass<GenericProp<Clearable, ValueType>> {
props = {}
setup() {
return {}
}
}
)

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