- 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 }
推薦閱讀
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- Extending Jenkins
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- Hadoop+Spark大數據分析實戰
- MySQL數據庫管理與開發實踐教程 (清華電腦學堂)
- Elasticsearch for Hadoop
- 利用Python進行數據分析
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- 時空數據建模及其應用
- Couchbase Essentials
- C語言程序設計
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- 青少年學Python(第2冊)
- Visual C++開發寶典