Skip to content
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

When defineComponent uses the function signature method, the expose function loses its type #10873

Closed
fslflame opened this issue May 6, 2024 · 4 comments

Comments

@fslflame
Copy link

fslflame commented May 6, 2024

Vue version

3.4.21

Link to minimal reproduction

https://github.com/fslflame/demo

Steps to reproduce

  1. The child component uses tsx file to return a rendering function. The defineComponent function signature method is used to expose the Expose for external use. The parent component obtains the ref instance of the child component and uses InstanceType to read the typeof the child component. The expose type does not exist. It cannot be inferred automatically, but it can be inferred automatically if the component uses defineExpose to expose data

What is expected?

defineExpose and expose should be consistent

What is actually happening?

  1. The child component uses tsx file to return a rendering function. The defineComponent function signature method is used to expose the Expose for external use. The parent component obtains the ref instance of the child component and uses InstanceType to read the typeof the child component. The expose type does not exist. It cannot be inferred automatically, but it can be inferred automatically if the component uses defineExpose to expose data

System Info

No response

Any additional comments?

No response

@fslflame fslflame changed the title defineComponent The expose type in ctx is lost in function signature writingff When defineComponent uses the function signature method, the expose function loses its type May 6, 2024
@fslflame
Copy link
Author

fslflame commented May 6, 2024

image
image
image

@yyx990803
Copy link
Member

I don't think there is a fix for this, since it's impossible to infer the type passed to expose in the return type of defineComponent in TypeScript.

The workaround is exporting your expose type as a separate interface:

import { defineComponent } from 'vue';

export inteface Exposed {
  getClipDataURL: number
}

export default defineComponent((_, { expose }) => {
  expose({
    getClipDataURL: 1,
  } as Exposed);
  return () => <div>8888</div>;
});

And in other components, import the Exposed interface directly instead of extracting it using InstanceType.

@yyx990803 yyx990803 closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2024
@fslflame
Copy link
Author

fslflame commented May 7, 2024

我认为这个问题没有解决办法,因为无法推断TypeScriptexpose的返回类型中传递的类型。defineComponent

解决方法是将公开类型导出为单独的接口:

import { defineComponent } from 'vue';

export inteface Exposed {
  getClipDataURL: number
}

export default defineComponent((_, { expose }) => {
  expose({
    getClipDataURL: 1,
  } as Exposed);
  return () => <div>8888</div>;
});

而在其他组件中,Exposed直接导入接口,而不是使用InstanceType.

image
Although this method is feasible, I need to get the component instance through InstanceType, if you specify Exposed then I can not get the component instance ref, is there no two ways?
Because the expose method is part of the component instance

@fslflame
Copy link
Author

fslflame commented May 7, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants