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

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.

主站蜘蛛池模板: 太白县| 招远市| 云霄县| 财经| 双鸭山市| 沂水县| 喜德县| 镶黄旗| 颍上县| 会理县| 浦县| 山东省| 通化市| 望奎县| 兴安盟| 府谷县| 新安县| 乐清市| 长春市| 通州区| 祥云县| 丽江市| 长沙市| 胶南市| 乌鲁木齐县| 武川县| 新和县| 邳州市| 巧家县| 平顺县| 肇庆市| 绵竹市| 通化市| 怀集县| 西畴县| 综艺| 彭泽县| 淮安市| 南丰县| 扶绥县| 措美县|