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

TypeScript does not infer SetupContext properly #2751

Closed
signor-pedro opened this issue Dec 7, 2020 · 6 comments
Closed

TypeScript does not infer SetupContext properly #2751

signor-pedro opened this issue Dec 7, 2020 · 6 comments
Labels
has workaround A workaround has been found to avoid the problem scope: types

Comments

@signor-pedro
Copy link

signor-pedro commented Dec 7, 2020

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 infer SetupContext<Record<string, any>> and instead infers SetupContext<{"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

@posva posva transferred this issue from vuejs/vue Dec 7, 2020
@posva
Copy link
Member

posva commented Dec 7, 2020

@signor-pedro moved to the correct repository

@posva posva added scope: types has workaround A workaround has been found to avoid the problem labels Dec 7, 2020
@signor-pedro
Copy link
Author

Looks like this might be related, will investigate later #2474

@sapphi-red
Copy link
Contributor

I think this behavior is correct because this update:modelValue indicates that it does not receive any parameters.
https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md#type-inference

  emits: {
    'update:modelValue': function works() { return true; },
  },

I think this should also infer SetupContext<{"update:modelValue": () => true}>.

Maybe #2362 is related?

  emits: {
    'update:modelValue'() { return true; },
  },

This may work for now. (though I think this also should infer SetupContext<{"update:modelValue": () => true}>)

@signor-pedro
Copy link
Author

signor-pedro commented Dec 9, 2020

@sapphi-red - if you are right and we should be inferring SetupContext<{"update:modelValue": () => true}>, how can I then type-hint the context properly?

All I wanted to do in my coposable is

function myFn({ emit }: SetupContext) {
   emit('sth', ...);
}

so that TypeScript (and in turn PHPStorm) infered the function signature for the emit function.

However, if I have to revisit the type hint every time I add a new emits, that sounds like I am not getting something about TypeScript... sorry, quite a TS newbie here 🤷

@sapphi-red
Copy link
Contributor

I think you should change it to SetupContext<any> if you want to be able to emit any event.

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 emit.

@LinusBorg
Copy link
Member

I think there is no way to infer SetupContext type from the usage of emit.

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

@github-actions github-actions bot locked and limited conversation to collaborators Oct 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem scope: types
Projects
None yet
Development

No branches or pull requests

4 participants