Skip to content

fix(custom-element): properly resolve props for sync component defs #12855

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

Merged
merged 5 commits into from
May 22, 2025
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
30 changes: 30 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,36 @@ describe('defineCustomElement', () => {
const e = container.childNodes[0] as VueElement
expect(e.shadowRoot!.innerHTML).toBe('hello')
})

test('prop types validation', async () => {
const E = defineCustomElement({
props: {
num: {
type: [Number, String],
},
bool: {
type: Boolean,
},
},
render() {
return h('div', [
h('span', [`${this.num} is ${typeof this.num}`]),
h('span', [`${this.bool} is ${typeof this.bool}`]),
])
},
})

customElements.define('my-el-with-type-props', E)
render(h('my-el-with-type-props', { num: 1, bool: true }), container)
const e = container.childNodes[0] as VueElement
// @ts-expect-error
expect(e.num).toBe(1)
// @ts-expect-error
expect(e.bool).toBe(true)
expect(e.shadowRoot!.innerHTML).toBe(
'<div><span>1 is number</span><span>true is boolean</span></div>',
)
})
})

describe('attrs', () => {
Expand Down
12 changes: 1 addition & 11 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,6 @@ export class VueElement
this._root = this
}
}

if (!(this._def as ComponentOptions).__asyncLoader) {
// for sync component defs we can immediately resolve props
this._resolveProps(this._def)
}
}

connectedCallback(): void {
Expand Down Expand Up @@ -391,12 +386,7 @@ export class VueElement
}
}
this._numberProps = numberProps

if (isAsync) {
// defining getter/setters on prototype
// for sync defs, this already happened in the constructor
this._resolveProps(def)
}
this._resolveProps(def)

// apply CSS
if (this.shadowRoot) {
Expand Down