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

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.

主站蜘蛛池模板: 左权县| 渝北区| 千阳县| 北票市| 托克逊县| 城固县| 塔河县| 黔东| 赫章县| 浠水县| 东海县| 高碑店市| 盐津县| 于都县| 思茅市| 东丰县| 夏邑县| 屏南县| 麟游县| 克拉玛依市| 隆安县| 三都| 广灵县| 伊吾县| 中山市| 紫金县| 盐池县| 凤城市| 乐至县| 扎赉特旗| 南雄市| 崇文区| 司法| 尉犁县| 农安县| 吐鲁番市| 五峰| 新沂市| 田阳县| 罗平县| 醴陵市|