ts的interface里面再写interface
时间: 2024-05-16 12:19:04 浏览: 81
在TS的interface中再写interface是指在一个interface的定义中,可以使用另一个interface作为其属性的类型。这样可以将多个interface组合在一起,实现更复杂的数据结构定义。以下是一个示例:
```typescript
interface Person {
name: string;
age: number;
}
interface Employee {
id: number;
department: string;
}
interface Manager {
employees: Employee[];
tasks: string[];
}
// 在Manager接口中使用了Person和Employee接口作为其属性的类型
interface ManagerWithPersonalInfo extends Person, Manager {
role: string;
}
const manager: ManagerWithPersonalInfo = {
name: "John",
age: 30,
employees: [
{ id: 1, department: "Sales" },
{ id: 2, department: "Marketing" }
],
tasks: ["Meeting", "Planning"],
role: "Manager"
};
```
在上面的示例中,ManagerWithPersonalInfo接口使用了Person和Manager接口作为其属性的类型,并额外定义了一个role属性。这样我们就可以将多个interface组合在一起,定义一个更具体的数据结构。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [TS Interface(接口)](https://blog.csdn.net/SeriousLose/article/details/117531317)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [04-TS接口interface、对象类型的使用](https://blog.csdn.net/xiaoleiv9/article/details/130965982)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [大小写金额转换-源码](https://download.csdn.net/download/chenxh/142447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文