官术网_书友最值得收藏!

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:

  1. 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[];
}
  1. 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.

主站蜘蛛池模板: 家居| 永仁县| 崇仁县| 揭阳市| 达日县| 广安市| 叶城县| 剑河县| 琼中| 永顺县| 章丘市| 贵港市| 隆化县| 中阳县| 昌平区| 永和县| 庆城县| 哈巴河县| 潼南县| 齐齐哈尔市| 嘉义市| 衡山县| 南开区| 孟村| 大埔县| 鹰潭市| 鹿泉市| 普兰店市| 兰溪市| 东阿县| 桃江县| 依安县| 建德市| 沂源县| 禹州市| 轮台县| 嵊泗县| 成都市| 鱼台县| 乌鲁木齐市| 衡阳县|