Skip to content

Commit a683c80

Browse files
authored
fix(custom-element): properly resolve props for sync component defs (#12855)
close #12854
1 parent 1d41d4d commit a683c80

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

packages/runtime-dom/__tests__/customElement.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,36 @@ describe('defineCustomElement', () => {
444444
const e = container.childNodes[0] as VueElement
445445
expect(e.shadowRoot!.innerHTML).toBe('hello')
446446
})
447+
448+
test('prop types validation', async () => {
449+
const E = defineCustomElement({
450+
props: {
451+
num: {
452+
type: [Number, String],
453+
},
454+
bool: {
455+
type: Boolean,
456+
},
457+
},
458+
render() {
459+
return h('div', [
460+
h('span', [`${this.num} is ${typeof this.num}`]),
461+
h('span', [`${this.bool} is ${typeof this.bool}`]),
462+
])
463+
},
464+
})
465+
466+
customElements.define('my-el-with-type-props', E)
467+
render(h('my-el-with-type-props', { num: 1, bool: true }), container)
468+
const e = container.childNodes[0] as VueElement
469+
// @ts-expect-error
470+
expect(e.num).toBe(1)
471+
// @ts-expect-error
472+
expect(e.bool).toBe(true)
473+
expect(e.shadowRoot!.innerHTML).toBe(
474+
'<div><span>1 is number</span><span>true is boolean</span></div>',
475+
)
476+
})
447477
})
448478

449479
describe('attrs', () => {

packages/runtime-dom/src/apiCustomElement.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,6 @@ export class VueElement
269269
this._root = this
270270
}
271271
}
272-
273-
if (!(this._def as ComponentOptions).__asyncLoader) {
274-
// for sync component defs we can immediately resolve props
275-
this._resolveProps(this._def)
276-
}
277272
}
278273

279274
connectedCallback(): void {
@@ -391,12 +386,7 @@ export class VueElement
391386
}
392387
}
393388
this._numberProps = numberProps
394-
395-
if (isAsync) {
396-
// defining getter/setters on prototype
397-
// for sync defs, this already happened in the constructor
398-
this._resolveProps(def)
399-
}
389+
this._resolveProps(def)
400390

401391
// apply CSS
402392
if (this.shadowRoot) {

0 commit comments

Comments
 (0)