- Learn React with TypeScript 3
- Carl Rippon
- 211字
- 2021-06-10 19:16:37
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:
- Firstly, let's export the Product interface:
export interface Product {
name: string;
unitPrice: number;
}
- 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.
- 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 }
- With this approach, we can also rename exported items using the as keyword:
interface Product {
name: string;
unitPrice: number;
}
export { Product as Stock }
推薦閱讀
- HTML5移動Web開發(fā)技術
- Android和PHP開發(fā)最佳實踐(第2版)
- Java深入解析:透析Java本質的36個話題
- Microsoft Dynamics GP 2013 Reporting, Second Edition
- iOS應用逆向工程(第2版)
- OpenShift在企業(yè)中的實踐:PaaS DevOps微服務(第2版)
- Mathematica Data Analysis
- 速學Python:程序設計從入門到進階
- .NET 4.5 Parallel Extensions Cookbook
- 區(qū)塊鏈國產化實踐指南:基于Fabric 2.0
- 從零開始學Selenium自動化測試:基于Python:視頻教學版
- R的極客理想:量化投資篇
- Getting Started with Web Components
- Swift Essentials(Second Edition)
- Django 3 Web Development Cookbook