- 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;
}
}
推薦閱讀
- Getting Started with Gulp(Second Edition)
- Java范例大全
- Python數據分析基礎
- DevOps for Networking
- KnockoutJS Starter
- INSTANT Passbook App Development for iOS How-to
- Unity UI Cookbook
- 大數據時代的企業升級之道(全3冊)
- Ext JS 4 Plugin and Extension Development
- 程序員的成長課
- 交互設計師成長手冊:從零開始學交互
- 算法精解:C語言描述
- WCF編程(第2版)
- 計算機常用算法與程序設計教程(第2版)
- C++從零開始學(視頻教學版)(第2版)