-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
TypeScript does not infer SetupContext properly #2751
Comments
@signor-pedro moved to the correct repository |
Looks like this might be related, will investigate later #2474 |
I think this behavior is correct because this emits: {
'update:modelValue': function works() { return true; },
}, I think this should also infer Maybe #2362 is related? emits: {
'update:modelValue'() { return true; },
}, This may work for now. (though I think this also should infer |
@sapphi-red - if you are right and we should be inferring All I wanted to do in my coposable is
so that TypeScript (and in turn PHPStorm) infered the function signature for the However, if I have to revisit the type hint every time I add a new |
I think you should change it to function myFn({ emit }: SetupContext<any>) {
emit('sth', ...);
} If you want to limit it to 'sth' event, do it like below. function myFn({ emit }: SetupContext<{ sth: (params: any) => any }>) {
emit('sth', ...);
} I think there is no way to infer SetupContext type from the usage of |
Yeah, I think there's not much to improve here as far as Vue's own types are concerned. If you want to limit your function to certain event types, you will have to explicitly type them. The same is true for props, for example. This is unrelated to #2474 |
Version
3.0.0
Reproduction link
https://codesandbox.io/s/vue3-example-hooks-ts-forked-uzeyf?file=/src/components/HelloWorldThatWorks.vue
Steps to reproduce
When the instance option
emits
is used with arrow-function validators (or in its simple array form), TypeScript fails to inferSetupContext<Record<string, any>>
and instead infersSetupContext<{"update:modelValue": () => true}>
(see the attached codesandbox example).What is expected?
That I can just typehint
{ emit }: SetupContext
in my composable.What is actually happening?
I need to update type definition whenever I add an
emits
(or use "full" functions which is ugly).(what you're looking for in the codesandbox is a TypeScript error. If you don't see one, wait a couple of seconds, usually it becomes visible after 15-20sec on the website - the
context
variable is underlined in red.)I don't know if this is the right place to report. If this is not a bug but deliberate, please let me know. Thanks
The text was updated successfully, but these errors were encountered: