I have a renderless component in JavaScript that I am trying to convert to TypeScript. I'm running into errors declaring the render function in a Vue.extend-ed component:
(method) ComponentOptions<Vue, unknown, unknown, unknown, never[], Record<never, any>>.render?(createElement: CreateElement, hack: RenderContext<Record<never, any>>): VNode
  No overload matches this call.
  The last overload gave the following error.
  Argument of type '{ render(): void; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
    Types of property 'render' are incompatible.
      Type '() => void' is not assignable to type '(createElement: CreateElement, hack: RenderContext<Record<string, any>>) => VNode'.
        Type 'void' is not assignable to type 'VNode'.ts(2769)
        vue.d.ts(90, 3): The last overload is declared here.
This is an example of what I am trying to do in TypeScript:
import Vue from 'vue'
export default Vue.extend({
  render() { // error happens when I add this. I tried typing the render with :VNode
             // and returning this.$scopedSlots.default({}), but error still occurs
  }
})
How do I fix this?
render() has this signature:
render?(createElement: CreateElement, hack: RenderContext<Props>): VNode;
Notes for render() declaration:
hack does not need to be declared in your codeVNode (i.e., a single root node), but Vue actually accepts VNode[] as a return (which is what this.$scopedSlots.default() returns)Solution 1: Specify the return type as VNode, and return a single node that wraps this.$scopedSlots.default():
import Vue, { VNode, CreateElement } from 'vue'
export default Vue.extend({
  render(h: CreateElement): VNode {
    return h('div', this.$scopedSlots.default!({}))
  }
})
Solution 2: Use any type assertion on this.$scopedSlots.default() to workaround the type error:
import Vue from 'vue'
export default Vue.extend({
  render() {
    // The container node is expected to provide a single root,
    // so it's okay to return `VNode[]` as any.
    return this.$scopedSlots.default!({}) as any
  }
})
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With