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

fix(types/custom-element): defineCustomElement with required props #11578

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
33 changes: 33 additions & 0 deletions packages/dts-test/defineCustomElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,37 @@ describe('defineCustomElement using defineComponent return type', () => {
expectType<number | undefined>(instance.a)
instance.a = 42
})

test('with required props', () => {
const Comp1Vue = defineComponent({
props: {
a: { type: Number, required: true },
},
})
const Comp = defineCustomElement(Comp1Vue)
expectType<VueElementConstructor>(Comp)

const instance = new Comp()
expectType<number>(instance.a)
instance.a = 42
})

test('with default props', () => {
const Comp1Vue = defineComponent({
props: {
a: {
type: Number,
default: 1,
validator: () => true,
},
},
emits: ['click'],
})
const Comp = defineCustomElement(Comp1Vue)
expectType<VueElementConstructor>(Comp)

const instance = new Comp()
expectType<number>(instance.a)
instance.a = 42
})
})
8 changes: 4 additions & 4 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type ComponentOptionsBase,
type ComponentOptionsMixin,
type ComponentProvideOptions,
type ComponentPublicInstance,
type ComputedOptions,
type ConcreteComponent,
type CreateAppFunction,
Expand Down Expand Up @@ -153,14 +154,13 @@ export function defineCustomElement<
// overload 3: defining a custom element from the returned value of
// `defineComponent`
export function defineCustomElement<
T extends DefineComponent<any, any, any, any>,
// this should be `ComponentPublicInstanceConstructor` but that type is not exported
T extends { new (...args: any[]): ComponentPublicInstance<any> },
>(
options: T,
extraOptions?: CustomElementOptions,
): VueElementConstructor<
T extends DefineComponent<infer P, any, any, any>
? ExtractPropTypes<P>
: unknown
T extends DefineComponent<infer P, any, any, any> ? P : unknown
>

/*! #__NO_SIDE_EFFECTS__ */
Expand Down