首页 > 解决方案 > Typescript 无法识别属性类型

问题描述

即使我为 activeNumber() 声明了类型,this.activeIndex + 1 也带有红色下划线,并且 VS 代码显示“运算符 '+' 不能应用于类型 '(() => any) | ComputedOptions' 和 'number'。 " 这怎么可能?我错过了什么?

import Vue from 'vue';

interface DataInterface {
  questions: [];
  results: [];
  activeIndex: number;
}

const Component = Vue.extend({
  props: {
    dataSrc: {
      type: String,
      default: null
    }
  },

  data(): DataInterface {
    return {
      questions: [],
      results: [],
      activeIndex: 0
    };
  },

  computed: {
    activeNumber(): number {
      return this.activeIndex + 1;
    }
})    
export default Component;

标签: typescriptvue.js

解决方案


因此,最终错误在重新启动计算机几次后消失了,就像评论中建议的 Ashish Deora 一样。我的同事建议你也可以在 VS code 中重新启动 Typescript 服务器。当您 ctrl+shift+P 并搜索“typescript”时,会出现此选项。这比重新启动要快得多。


推荐阅读