Typescript无法使用vue.js查看此类型

时间:2018-03-01 09:52:34

标签: visual-studio typescript webpack vue.js

我有这段代码:

const c = Vue.extend({
    data() {
        return {
            msg: 'Hello'
        }
    },
    methods: {
        greet(): string {
            this.msg = "asdasd"; // Type '"asdasd"' is not assignable to type 'Function'.
            return this.msg + ' world';
        }
    }
});

this.msg = "asdasd"显示错误:Type '"asdasd"' is not assignable to type 'Function'.。这在VS和webpack构建期间都会发生。感觉就像打字稿不能正确看到这种类型,即使我使用Vue.extend这可能是什么原因?

我的tsconfig.json

{
  "compilerOptions": {
    "outDir": "./built/",
    "sourceMap": true,
    "strict": true,
    "noImplicitReturns": true,
    "module": "es2015",
    "moduleResolution": "node",
    "target": "es5",
    "allowSyntheticDefaultImports": true,
  },
  "include": [
    "./ViewModels/**/*"
  ]
}

我还从.csproj文件中删除了所有提到的typescript,因为VS无法构建typescript(我不想要它,因为我还是使用webpack来构建它)

Webpack配置:https://gist.github.com/archeg/743348021c59563a9d479993bc0c349f

1 个答案:

答案 0 :(得分:0)

对于那些正在努力解决同一问题的人:我切换到https://github.com/vuejs/vue-class-component并开始正常工作。

例如,我发布的代码与vue-class-component

一样
import Vue from "vue";
import Component from 'vue-class-component';

@Component({})
class Example extends Vue {
    msg = "Hello";
    greet(): void {
      this.msg = "asdasd"; //This is a proper here.
      return this.msg + ' world';
    }
}