【发布时间】:2020-12-09 09:40:50
【问题描述】:
我最近开始使用 Nuxt-ts 和 Vue 2 在 Typescript 中进行开发。我试图避免使用 class-style components 编写应用程序,所以我使用了以下解决方案。
我在名为/types 的文件夹中声明我的接口。如果我想在 Vue 组件的data 中声明一个type,我会导入该类型,然后在组件中使用它。
// types/Fruit.ts
export interface Fruit {
name: string,
color: string,
taste: string
}
// components/FruitComponent.vue
<script lang="ts">
import Vue from 'vue'
import { Fruit } from '@/types/fruits'
export default Vue.extend({
name: 'FruitComponent',
data() {
return {
fruit: {
name: 'apple',
color: 'red',
taste: 'delicious',
wrongParameter: 'this should be an error',
} as Fruit
}
}
})
</script>
问题是,编译器和可视化代码编辑器不会捕捉到如上所示的错误。
我预计会出现类似于 Object literal wrongParameter is not assignet to type 'Fruit' 的错误。
我应该如何正确地捕捉这种类型的错误?
【问题讨论】:
标签: typescript vue.js vuejs2 nuxt.js