【发布时间】:2020-07-24 13:29:22
【问题描述】:
我是 TypeScript 的新手。下面的代码在 JavaScript 上运行没有问题,但是这里没有编译。
export default {
data: function() {
return {
data: [
'Angular',
'Angular 2',
'Aurelia',
'Backbone',
'Ember',
'jQuery',
'Meteor',
'Node.js',
'Polymer',
'React',
'RxJS',
'Vue.js'
],
name: "",
selected: null,
hasVariationRadio: ""
};
},
computed: {
filteredDataArray(): [] {
return this.data.filter((option: string) => { // This is where the error appears
return option
.toString()
.toLowerCase()
.indexOf(this.name.toLowerCase()) >= 0 // Also here.
})
}
}
};
我收到以下错误:
Property 'name' does not exist on type '{ filteredDataArray(): []; }'.
如here 所述,strict 在 tsconfig.json 文件中的 compilerOptions 内设置为 true。
【问题讨论】:
-
我无法重现您在我的设置中提到的错误(选择了带有 TypeScript 选项的 Vue CLI 项目)。我得到一个不同的错误:
Type 'string[]' is not assignable to type '[]',但是将filteredDataArray()的返回类型设置为string[]可以解决它。
标签: typescript vue.js