Skip to content

Commit

Permalink
fix(types/custom-element): defineCustomElement with required props (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andylizi authored and yyx990803 committed Aug 16, 2024
1 parent 8bcaad4 commit 5e0f6d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
33 changes: 33 additions & 0 deletions packages-private/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

0 comments on commit 5e0f6d5

Please sign in to comment.