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

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.

主站蜘蛛池模板: 天长市| 神木县| 浙江省| 通化县| 博爱县| 湖口县| 铜陵市| 闽清县| 万安县| 浦江县| 怀仁县| 甘孜| 贡山| 山丹县| 襄樊市| 贵阳市| 高阳县| 广宁县| 广宗县| 马山县| 南宫市| 清徐县| 莒南县| 曲阳县| 抚远县| 错那县| 贡嘎县| 东丽区| 兖州市| 石泉县| 驻马店市| 营山县| 兰州市| 靖边县| 开江县| 息烽县| 桂林市| 南城县| 龙游县| 连山| 玉溪市|