11

I'm starting to learn typescript & Vuejs.

Can anyone explain me why I can't access the account property in data from the computed allChecked()?

import * as Vue from "vue";

declare var accounts: any[];    

var app = new Vue({
    el: '#vueowner',
    data: {
        accounts: accounts,
        hasAccount: this.accounts.length > 0,
        checkedAccounts: []
    },
    computed: {
        allChecked() {
            return this.accounts.length === this.checkedAccounts.length;
        }
    }
})

I have this errors

ERROR in index.ts
(25,25): error TS2339: Property 'accounts' does not exist on type 'Vue'.

ERROR in index.ts
(25,50): error TS2339: Property 'checkedAccounts' does not exist on type 'Vue'.
2
  • what is in your 'vue' imported file ? a d.ts file ? Commented Jun 20, 2017 at 13:17
  • imports are the ones from Vuejs 2 typings Commented Jun 20, 2017 at 13:20

3 Answers 3

15

Looks like you need to annotate the return types because TypeScript has difficulties inferring the types of certain methods.

so instead of

allChecked() {
   return this.accounts.length === this.checkedAccounts.length;
}

try this

allChecked(): boolean {
   return this.accounts.length === this.checkedAccounts.length;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer. Their documentation even says that you have to annotate return types for computed properties and render functions: vuejs.org/v2/guide/typescript.html#Annotating-Return-Types
2

In your allChecked method the this keyword isn't referencing the option object you are passing to the Vueconstructor, but the instance of the Vue class itself.

You will need to create a class that extends Vue with the properties you wish to add to the original class :

import * as Vue from "vue";

class MyVue extends Vue {
    accounts: any[];
    checkedAccounts: any[];
}

const app = new MyVue({
    // (...)
})

If you need more information about using Vue.js with Typescript check out this page: https://v2.vuejs.org/v2/guide/typescript.html

2 Comments

If true, why this line is working in the data section: hasAccount: this.accounts.length > 0?
I would guess that it is compiling (since this should have the type any right there) but not working as you intend it to (probably a Cannot read property 'length' of undefined at runtime). It should definitely be hasAccounts: accounts.length.
0

This is a good article to start with VuejS with Typescript

https://johnpapa.net/vue-typescript/

<template>

</template>
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';

  export default class App extends Vue {
    public accounts: any = []; 
    public checkedAccounts: any = [];
    public created(): void {
        this.accounts = [];
        this.checkedAccounts= [];
    }
   public allChecked(): boolean {
    return this.accounts.length === this.checkedAccounts.length;
   }
  }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.