- Learn React with TypeScript 3
- Carl Rippon
- 162字
- 2021-06-10 19:16:37
Importing
Importing allows us to import items from an exported module. We do this using an import statement that includes the item names to import in curly braces and the file path to get the items from (excluding the ts extension). We can only import items that are exported in the other module file.
- Let's resolve the issue with our OrderDetail class by importing the Product interface:
import { Product } from "./product";
class OrderDetail {
product: Product;
quantity: number;
getTotal(discount: number): number {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}
}
- We can rename imported items using the as keyword in an import statement. We then reference the item in our code using the new name:
import { Product as Stock } from "./product";
class OrderDetail {
product: Stock;
quantity: number;
getTotal(discount: number): number {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}
}
推薦閱讀
- Learning Python Web Penetration Testing
- Mastering Visual Studio 2017
- Java 9 Concurrency Cookbook(Second Edition)
- Game Programming Using Qt Beginner's Guide
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Python Game Programming By Example
- FreeSWITCH 1.6 Cookbook
- Java面向對象程序設計
- Android群英傳
- ActionScript 3.0從入門到精通(視頻實戰版)
- Application Development with Swift
- Visual C++程序設計與項目實踐
- Spring Data JPA從入門到精通
- Swift High Performance
- ANSYS FLUENT 16.0超級學習手冊