- 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.
推薦閱讀
- Git高手之路
- JavaScript前端開發(fā)與實例教程(微課視頻版)
- Scientific Computing with Scala
- Unreal Engine 4 Shaders and Effects Cookbook
- Learning Python Design Patterns
- Building Microservices with .NET Core
- Android開發(fā)三劍客:UML、模式與測試
- Web前端開發(fā)最佳實踐
- IBM RUP參考與認(rèn)證指南
- Building Apple Watch Projects
- Moodle 3.x Developer's Guide
- Swift Essentials(Second Edition)
- Expert Cube Development with SSAS Multidimensional Models
- PHP典型模塊與項目實戰(zhàn)大全
- PHP高性能開發(fā):基礎(chǔ)、框架與項目實戰(zhàn)