- Learn React with TypeScript 3
- Carl Rippon
- 169字
- 2021-06-10 19:16:34
Extending interfaces
Interfaces can extend other interfaces so that they inherit all the properties and methods from its parent. We do this using the extends keyword after the new interface name and before the interface name that is being extended.
Let's look at the following example:
- We create a new interface, taking Product as a base, and add information about discount codes:
interface Product {
name: string;
unitPrice: number;
}
interface DiscountCode {
code: string;
percentage: number;
}
interface ProductWithDiscountCodes extends Product {
discountCodes: DiscountCode[];
}
- We can create an instance of the interface in the usual way, filling in properties from the base interface as well as the child interface:
const table: ProductWithDiscountCodes = {
name: "Table",
unitPrice: 500,
discountCodes: [
{ code: "SUMMER10", percentage: 0.1 },
{ code: "BFRI", percentage: 0.2 }
]
};
Interfaces allow us to create complex but flexible structured types for our TypeScript program to use. They are a really important feature that we can use to create a robust, strongly-typed TypeScript program.
推薦閱讀
- Debian 7:System Administration Best Practices
- 編程珠璣(續)
- 新編Premiere Pro CC從入門到精通
- Python Network Programming Cookbook(Second Edition)
- Drupal 8 Configuration Management
- UVM實戰
- 響應式架構:消息模式Actor實現與Scala、Akka應用集成
- Deep Learning with R Cookbook
- Python開發基礎
- 實驗編程:PsychoPy從入門到精通
- Learning Alfresco Web Scripts
- 前端架構設計
- Java核心技術速學版(第3版)
- Python量子計算實踐:基于Qiskit和IBM Quantum Experience平臺
- Head First Go語言程序設計