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

Property '[...]' does not exist on type 'VueConstructor<Vue>' #11759

Closed
Byloth opened this issue Nov 9, 2020 · 3 comments · May be fixed by #11762
Closed

Property '[...]' does not exist on type 'VueConstructor<Vue>' #11759

Byloth opened this issue Nov 9, 2020 · 3 comments · May be fixed by #11762

Comments

@Byloth
Copy link

Byloth commented Nov 9, 2020

Version

2.6.12

Reproduction link

https://codepen.io/Byloth/pen/mdEzMGX

N.B.: It works, here on CodePen... But it won't work inside a real Vue project.

Steps to reproduce

  1. Create a new TypeScript Vue Project using Vue CLI.
  2. Copy & Paste this mixin inside main.ts file (right above Vue app initialization and mount).
Vue.mixin({
    data: () => ({ aRandomMixinProperty: "John Doe" }),

    mounted()
    {
        this.aRandomMixinProperty = "John Snow";
    }
});
  1. Try to build the project.

What is expected?

It should compile correctly.

What is actually happening?

It doesn't compile at all.

ERROR in /path/to/project/src/main.ts(15,14):
15:14 Property 'aRandomMixinProperty' does not exist on type 'VueConstructor<Vue> | ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<...>, Record<...>>'.
  Property 'aRandomMixinProperty' does not exist on type 'VueConstructor<Vue>'.
    13 |     mounted()
    14 |     {
  > 15 |         this.aRandomMixinProperty = "John Snow";
       |              ^
    16 |     }
    17 | });
    18 |
Version: typescript 4.0.5
Time: 2856ms

As workaround, you could use:

/* main.ts */

Vue.mixin({
    data: () => ({ aRandomMixinProperty: "John Doe" }),

    mounted()
    {
        (this as Vue).aRandomMixinProperty = "John Snow";
    }
});

... and...

/* mixins.d.ts */

declare module "vue/types/vue"
{
    interface Vue
    {
        aRandomMixinProperty: string;
    }
}
@posva
Copy link
Member

posva commented Nov 9, 2020

The workaround you mentioned is the way to go: you need to tell TS that global properties have been added

@posva posva closed this as completed Nov 9, 2020
@Byloth
Copy link
Author

Byloth commented Nov 9, 2020

Oh... Ok, @posva!

But... Why doesn't the typings work like the extend method?
Why data is ignored?

Aren't the typings rules written correctly, is it by choice or is it simply impossible to define them correctly?
May I contribute to fix this problem? Could it be worth investing some time?

@seupedro
Copy link

seupedro commented Mar 22, 2021

For those coming from Google like me, check this thread instead #8721.
There are some workarounds like this, this, this, and this.

And for those who likes to take time read and understand, check this reply on same thread.

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

Successfully merging a pull request may close this issue.

3 participants