-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
In the TypeScript component, the error Property does not exist on type 'Vue' #8406
Comments
Duplicate of #6841 😕 |
Official documentation is really too little for TypeScript |
I got the same errors with vue 2.8.2 and Typescript 2.5.3. I fixed it by holding my Vue instance in a variable then giving it a type. This ensures TS will know about all Vue properties when you instatiate it using an options object.
|
The trick is that it is TypeScript, you'll need to define an interface that your component implements. This is the blessing and curse of Statically Types languages. Take for instance this example with my Nav component in Vue: <template lang="pug">
nav.navbar(role="navigation" aria-label="main navigation")
<!-- Branding and Hamburger -->
.navbar-brand
a.navbar-item(href="https://bulma.io")
img(src="https://bulma.io/images/bulma-logo.png" alt="Bulma: a modern CSS framework based on Flexbox" width="112" height="28")
a.navbar-burger(role="button" aria-label="menu" aria-expanded="false")
span(aria-hidden="true")
span(aria-hidden="true")
span(aria-hidden="true")
<!-- Navbar Menu -->
<!-- Hidden on mobile, needs .is-active added at those sizes -->
div(v-bind:class="{navBarMenuClass}")
</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
const navbarMenuBaseClass = 'navbar-menu';
interface INav {
menuIsActive: boolean;
}
@Component
export default class Nav extends Vue implements INav {
menuIsActive: boolean = false;
computed() {
return {
navbarMenuClass: navbarMenuBaseClass + (this.menuIsActive ? 'is-active' : '')
}
}
}
</script> Until I added the |
That helped. Thanks. But that is not a valid and scalable solution, if you have tons of components... $router, $store, etc. are being injected into Vue Type in main.js with new Vue({...}). TypeScript cannot know that they are present in 'this'/Vue, because Vue Interface in vue.d.ts does not cover these keys. I think the Vue Interface in vue.d.ts should have optional parameters for those plugins(vuex, vue-router), so TS knows that they ($store, $router) could be present. |
I opened the vue.d.ts and seen this extend<Data, Methods, Computed, PropNames extends string = never>(options?: ThisTypedComponentOptionsWithArrayProps<V, Data, Methods, Computed, PropNames>): ExtendedVue<V, Data, Methods, Computed, Record<PropNames, any>>; this solved my issue |
You can type Data, Methods, Computed and Props like this:
This way the Typescript errors should go away. |
TypeScript can understand most of those types on it's own, but even if it is not used where the error is shown, all the I just spent hours to find out that if I simply put It's writen here: https://vuejs.org/v2/guide/typescript.html#Annotating-Return-Types |
I fix this by using defineComponent my code was like this
and I have error "property message does not exist on type..." then I fix it with
errors gone and work fine :) |
Version
2.5.16
Reproduction link
https://codesandbox.io/s/github/weihongyu12/vue-typescript-demo
Steps to reproduce
Uses @vue/cli to build the project. The configuration uses TypeScript. However, in the process of using, in the
methods
, there is no access to thecomputed
. The construction does not exist on type 'Vue'.What is expected?
I don't know if it's because of my writing, or because of the configuration that caused me this error. I wrote the code based on the official document. I believe it can be build successfully.
What is actually happening?
When I run
npm run build
, an error Property 'pickerMap' does not exist on type 'Vue'. The complete prompt is as follows:I do not know of any reason for this error. It is imperative that I have a friend to help me solve this error, but I hope that the official can speed up the improvement of TypeScript's documentation, because I believe TypeScript will become popular. Thank you.
The text was updated successfully, but these errors were encountered: