- 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.
推薦閱讀
- 零基礎學Visual C++第3版
- Essential Angular
- Mastering Unity Shaders and Effects
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- Scratch3.0趣味編程動手玩:比賽訓練營
- Image Processing with ImageJ
- Android系統下Java編程詳解
- Android應用開發實戰(第2版)
- UX Design for Mobile
- Scratch編程從入門到精通
- 菜鳥成長之路
- Mastering React Test:Driven Development
- Python編程零基礎入門
- MATLAB語言及編程實踐:生物數學模型應用
- Neo4j High Performance