Subscribe on changes!

InstanceType doesn't see exposed properties for components with `setup` returning render function

avatar
Sep 22nd 2023

Vue version

3.3.4

Link to minimal reproduction

https://play.vuejs.org/#eNrNVFFv2jAQ/itWXggSJILuiQHqNvHQPWzV1kdLU5Yc4C6xLdtJmVD+++6cNCQU1GpPBSmx7z7fffddzsfgk9ZRVUKwCJY2NUI7ZsGVmuWJ3K144CwP1lyKQivj2JEZ2LKabY0q2AiPjT52vi+q0K0jimlDYUcD92zgn70EzAeAeQvgMlXSOpZShhVRWN7hPpEpPPzVsHT4UFsfYL0OxwSn7EleQuTAuv752SsBZqcIyO9iiPkrIeanEFhBL8QybhReB5OgKXpaJDp6tEqi/EcuGeOtA1VfMG8hG8pAex7sndN2EcdpJvFYBrmoTCTBxVIX8S3CYlNKJwqYZqq4vYluog9xJqzrmyOwxfS3UU8WDAbhwaSXJkZjBWZqQGZgwLw17dmxfuoz14v0lL3mskZRnEWVt2J3JgkpKXIw37UT2IWBNEmeq6ev3uZMCV0t6R7SPxfsj/bQ1HRvwDPr1e8SswPXuDc/v8EB152zUFmZt2244vwBVuUlcWxgn0uZIe0ezrO98x0WcvdgNwcH0j4XRUS9Gh7v+0Hf07XST3RR7Z6Kz7PXG+krw5zBVkifQ0mQbsL2g9mmyYODxyIyKXP/7p8IPRl/YYS/JhiR4BZYPSaa9CeqjbHB0o+mYcHCZMGsM6jDmK3W7B7TCgsRtkXlFYQJjhCBa3w3K4NpjGShh+/DUSaqkQdR3bjoxovLVgV/w7wbGVr+/ylDE+Vqqf6uvFxqQwyhHCeQeG+6fryZwnnS+h9mXh7c

Steps to reproduce

Create a component with options API. In setup call an expose and return a render function

<script lang="ts">
import { defineComponent, h } from 'vue'

export default defineComponent({
  setup(_, { expose }) {
    expose({
      test: (a: string) => Promise.resolve(a)
    })
    return () => h('div')
  }
})
</script>

Import it in another component and take its type via InstanceType

<script setup lang="ts">
import { ref } from 'vue';
import Comp from './Comp.vue'

const comp = ref<InstanceType<typeof Comp>>()
comp.value.test
</script>

What is expected?

Exposed properties are present in interface

What is actually happening?

Exposed properties are not present in interface

System Info

No response

Any additional comments?

No response

avatar
Sep 23rd 2023

This is a limitation in typescript. These exposed variables cannot be known in type-level since they are bound to your runtime code

avatar
Sep 24th 2023

Yes, const comp = ref<InstanceType<typeof Comp>>() is only responsible for type safety and can ensure that the type is correct when you write code. Vue will not generate its object based on this type during the compilation phase and pass it to the ref function to define a reactive object.

avatar
Sep 25th 2023

@baiwusanyu-c Why Vue will not generate its object based on this type during the compilation phase? Why it's not planned? Do you plan to deprecate render-functions?

avatar
Sep 25th 2023

What you said makes sense and clear for me. But I'm talking not about inferring types but about generating types. I mean watch components during development and dynamically generate types.

avatar
Sep 25th 2023

@baiwusanyu-c @so1ve But there is more important question. Are you going to support and develop render-functions in terms of TS support? Or it's something that is going to be deprecated for everyday usage?