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

Exporting

Exporting code from a module allows it to be used by other modules. In order to export from a module, we use the export keyword. We can specify that an item is exported using export directly before its definition. Exports can be applied to interfaces, type aliases, classes, functions, constants, and so on.

Let's start to adjust our example code from the previous section to operate in modules rather than the global scope:

  1. Firstly, let's export the Product interface:
export interface Product {
name: string;
unitPrice: number;
}
  1. After we make this change, the compiler will complain about the reference to the Product interface in the OrderDetail class:

This is because Product is no longer in the global scope but OrderDetail still is. We'll resolve this in the next section, but let's look at alternative ways we can export the Product interface first.

  1. We can use an export statement beneath the item declarations. We use the export keyword followed by a comma-delimited list of item names to export in curly braces:
interface Product {
name: string;
unitPrice: number;
}

export { Product }
  1. With this approach, we can also rename exported items using the as keyword:
interface Product {
name: string;
unitPrice: number;
}

export { Product as Stock }
主站蜘蛛池模板: 同德县| 吉水县| 四子王旗| 湘西| 台安县| 黑山县| 玛纳斯县| 开平市| 鸡西市| 肥城市| 皋兰县| 庐江县| 拉孜县| 大庆市| 萝北县| 淮南市| 正阳县| 门头沟区| 西吉县| 灌南县| 泗阳县| 靖江市| 张北县| 邮箱| 九龙坡区| 正镶白旗| 海门市| 贵州省| 彭泽县| 禹州市| 清水县| 宿迁市| 于都县| 虞城县| 阳江市| 青神县| 武清区| 金门县| 白银市| 西贡区| 卢龙县|