- 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.
推薦閱讀
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- Learn Scala Programming
- The Computer Vision Workshop
- Full-Stack Vue.js 2 and Laravel 5
- 教孩子學(xué)編程:C++入門圖解
- ADI DSP應(yīng)用技術(shù)集錦
- C#程序設(shè)計
- 移動界面(Web/App)Photoshop UI設(shè)計十全大補
- Extreme C
- Android Development Tools for Eclipse
- Mastering Elixir
- OpenCV Android開發(fā)實戰(zhàn)
- Photoshop智能手機APP界面設(shè)計
- Mastering Concurrency in Python
- Arduino電子設(shè)計實戰(zhàn)指南:零基礎(chǔ)篇