我有介面:
export interface TaskInterface{ name: string description1: string time: string }
和組件
import { TaskInterface } from '@/types/task.interface'
data () {
return {
tasks: [
{
name: 'Create app',
description1: 'Use smth ',
time: '02.12.2021'
},
{
name: 'Fix bugs',
description1: 'Fix all bugs',
time: '02.12.2021' }] as TaskInterface[]
但如果名稱:123,則不會出錯,
如何為變數添加介面?
uj5u.com熱心網友回復:
您可以通過創建一個任務變數并明確說明它的型別來輕松實作這一點。通過這種方式,您可以讓打字稿知道TaskInterface任務陣列中允許包含屬性的所有物件。
<script lang="ts">
import { defineComponent } from "vue";
import { TaskInterface } from '@/types/task.interface'
export default defineComponent({
name: "Home",
data() {
const tasks: TaskInterface[] = [
{
name: "Create app",
// name: 123, // Type 'number' is not assignable to type 'string'.
description1: "Use smth ",
time: "02.12.2021",
},
{
name: "Fix bugs",
description1: "Fix all bugs",
time: "02.12.2021",
},
];
return {
tasks,
};
},
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/346181.html
