TypeScript error when forwarding ctx.attrs to child component via h() #14507
Replies: 3 comments 2 replies
|
@shubph The issue is that return h(Child, ctx.attrs as any);
// or simpler in setup:
return () => <Child {...(ctx.attrs as any)} />Since you are disabling |
|
So the cleanest fix depends on what contract you want for the wrapper. If import { defineComponent, h } from 'vue'
const childProps = {
name: { type: String, required: true },
label: String,
} as const
const Child = defineComponent({
props: childProps,
template: `<div>{{ name }}</div>`,
})
export const Wrapper = defineComponent({
props: childProps,
inheritAttrs: false,
setup(props, { attrs }) {
return () => h(Child, { ...attrs, ...props })
},
})That makes If the wrapper is intentionally transparent and you want to delegate whatever came in through import type { ExtractPublicPropTypes } from 'vue'
type ChildPublicProps = ExtractPublicPropTypes<typeof childProps>
return () => h(Child, ctx.attrs as ChildPublicProps)That cast is basically saying: "this wrapper does not validate these props; the child does." The fully type-safe version is the first approach, where the wrapper declares the props it accepts. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I’m running into a TypeScript issue when trying to forward
ctx.attrsfrom a wrapper component into a typed child component usingh().Reproduction
I'm not sure how I would resolve this, any ideas?
All reactions