Vue +打字稿(不适用于带有打字稿的道具类型)

时间:2020-09-02 06:12:05

标签: typescript vue.js

我将错误的项目类型传递给“ ListItem”组件,但我的打字稿没有抛出错误。

:item =“ {哈哈:'string'}”作为其他类型,但应为{id:1,标题:'买面包',完成:false}作为TodoItem类型

组件TodoList:

<template>
   <div class="container">
        <ol>
          <list-item v-for="t of todos" :item="{ lol: 'string' }" :key="t.id"/>
        </ol>
   </div>
</template>

<script lang="ts">
      import ListItem from '@/components/ListItem.vue'
      import { ListState } from '@/types/home-page'
      import { reactive, defineComponent } from 'vue';
    
      export default defineComponent({
        name: "TodoList",
        components: {
          ListItem
        },
        setup () {
          const state = reactive<ListState>({
            todos: [
              { id: 1, title: 'Buy bread', completed: false },
              { id: 2, title: 'Wash the dishes', completed: true },
              { id: 3, title: 'clean the room', completed: false },
            ]
          });
    
          return { ...state };
        }
      });
    </script>

组件ListItem:

<template>
    <li> {{ item }} </li>
</template>

<script lang="ts">
  import { TodoItem } from '@/types/home-page'
  import { PropType, defineComponent } from 'vue'

  export default defineComponent({
    name: "ListItem",
    props: {
      item: Object as PropType<TodoItem>,
    }
  });
</script>

文件类型:

export interface TodoItem {
  id: number;
  title: string;
  completed: boolean;
}

export interface ListState {
  todos: TodoItem[];
}

0 个答案:

没有答案