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

Bug: Type-check doesn't work properly for variables declared in script setup components #1866

Closed
gbyesiltas opened this issue Nov 15, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@gbyesiltas
Copy link

gbyesiltas commented Nov 15, 2022

Describe the bug
Trying to access a variable declared in a component written with script setup results in a type error

Property 'someVariable' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: Partial<{ errors: string[]; }> & Omit<Readonly<ExtractPropTypes<{ action: { type: PropType<"Update" | "Create">; required: true; }; ... 5 more ...; isLoading: { ...; }; }>> & ... 5 more ... & ComponentCustomProps, "errors">; ... 10 more ...; $watch<T extends string ...'.

To Reproduce

  1. Create a component with script setup and declare a variable inside it
  2. Write a test where you are trying to access that variable with wrapper.vm.someVariable
  3. The type-check will give an error similar to the one above

Expected behavior
The variable accesses to work without typing errors

Related information:

  • @vue/test-utils version: 2.2.3
  • Vue version: 3.2.45
  • node version: 16.17.0
  • npm (or yarn) version: 9.1.1

Additional context
The tests do pass, but typescript isn't happy

@gbyesiltas gbyesiltas added the bug Something isn't working label Nov 15, 2022
@cexbrayat
Copy link
Member

Hi @gbyesiltas

Yes, you'll need to add an explicit cast, like expect((wrapper.vm as unknown as { count: number }).count).toBe(1).

We sadly can't do anything about it in VTU, we're waiting for improvements in Vue itself.

This is tracked in #972

Closing this one as duplicate

@cexbrayat cexbrayat closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2022
@gbyesiltas
Copy link
Author

gbyesiltas commented Nov 15, 2022

Hi @gbyesiltas

Yes, you'll need to add an explicit cast, like expect((wrapper.vm as unknown as { count: number }).count).toBe(1).

We sadly can't do anything about it in VTU, we're waiting for improvements in Vue itself.

This is tracked in #972

Closing this one as duplicate

Hey, thanks for the quick reply 👍 Won't be the prettiest thing but It will do :) Hope Vue can improve support in the future.

Also, another question, this only started happening after we made the upgrades vue: 3.2.41 --> 3.2.45 and @vue/test-utils: 2.0.0 --> 2.2.3. Is it then related to some changes that happened in the Vue side?

@cexbrayat
Copy link
Member

Yeah I hope too 😉

This is the case for a while, but if you were still on VTU v2.0.0, maybe we had a any type for vm, whereas we now try to infer the maximum information we can (but I don't exactly remember in which version we started to improve this).

@gbyesiltas
Copy link
Author

Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants