D
D
Daniil Popov2020-12-04 22:51:48
typescript
Daniil Popov, 2020-12-04 22:51:48

How to format calculated properties with TS correctly so that the linter does not swear?

There is a computed property with a getter and a setter that don't understand what this is. The code itself works correctly

Error
5fcaa0dcab3fc527241195.png

Code

import Vue from "vue"
  import { mapMutations } from "vuex";

  export default Vue.extend({
    name: "v-component",
    props: {
      text: {
        type: String
      }
    },
    computed: {
      computedProperty: {
        get(): string {
          return this.text;
        },
        set(value) {
          this.someMethod(value);
        }
      }
    },
    methods: {
      ...mapMutations({
        someMethod: "SOME_METHOD"
      })
    }
  })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex K, 2020-12-05
@groog

Set an explicit "this" definition for the getter/setter. This is done as the first "this" parameter of the function signature. The parameter in this case is fake, that is, it is used only to determine the type "this". For example:

get(this: {text: string}): string {
   return this.text;
},
set(this: {someMethod: (value: string) => void}, value: string) {
   this.someMethod(value);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question